使用 JavaScript 设置 URL 中隐藏参数的值
我正在尝试从 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="">
但这对我不起作用。这里出了什么问题???有更好的办法吗?
I'm trying to set the hidden field for 'item_number' from the Url QueryString for a paypal form.
So the URL will look like this "http://website.com/customize.aspx?item_number=FFFF"
and code:
<script language="javascript" type="text/javascript">
document.getElementById('item_number').Value = Request.QueryString('item_number');
</script>
<input type="hidden" name="item_number" value="">
But this doesnt work for me. Whats wrong here???? is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getElementById
仅通过 ID 查找元素。您的隐藏项没有item_number
的id
;然而,它有这个名字。如果您将id="item_number"
添加到您的input
中,那么该代码应该可以工作。您还需要将脚本移至 DOM 元素之后。否则,它将在文档中存在输入
之前运行。更新
刚刚注意到另一个错误。您正在设置
Value
属性,并且Request.QueryString('item_number')
也无效。您似乎将 ASP.NET 代码与 JavaScript 混淆了。隐藏输入的正确属性名称是value
(小写)。 JavaScript 中没有Request.QueryString
的等效项。相反,要提取查询字符串值,请参阅此答案以获得这样做的好方法。getElementById
only finds elements by their ID. Your hidden doesn't have theid
ofitem_number
; it has that name, however. If you addid="item_number"
to yourinput
, then the code should work. You also need to move your script to after the DOM element. Otherwise, it will run before theinput
exists in the document.Update
Just noticed another mistake. You're setting a
Value
property, andRequest.QueryString('item_number')
is also invalid. It looks like you're confusing ASP.NET code with JavaScript. The correct property name for the hidden input isvalue
(lowercase). There is no equivalent ofRequest.QueryString
in JavaScript. Rather, to extract query string values, see this answer for a good way to do so.