使用javascript设置URL中隐藏参数的值

Set the value for a hidden parameter from the URL using javascript

本文关键字:参数 隐藏 javascript 设置 URL 使用      更新时间:2023-09-26

我正试图从paypal表单的Url QueryString中为"item_number"设置隐藏字段。

因此URL将如下所示"http://website.com/customize.aspx?item_number=FFFF"

和代码:

<script language="javascript" type="text/javascript">
document.getElementById('item_number').Value = Request.QueryString('item_number');
</script>
<input type="hidden" name="item_number" value="">

但这对我不起作用。这里出了什么问题????有更好的方法吗?

getElementById只根据元素ID查找元素。您的隐藏没有item_numberid;然而,它有这个名字。如果将id="item_number"添加到input中,那么代码应该可以工作。您还需要将脚本移动到DOM元素之后的。否则,它将在文档中存在input之前运行。

更新

刚刚注意到另一个错误。您正在设置Value属性,而Request.QueryString('item_number')也是无效的。看起来您将ASP.NET代码与JavaScript混淆了。隐藏输入的正确属性名称是value(小写)。在JavaScript中没有等效的Request.QueryString。相反,要提取查询字符串值,请参阅此答案以获得一个很好的方法