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

发布于 2025-01-02 15:42:23 字数 428 浏览 3 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酸甜透明夹心 2025-01-09 15:42:23

getElementById 仅通过 ID 查找元素。您的隐藏项没有 item_numberid;然而,它有这个名字。如果您将 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 the id of item_number; it has that name, however. If you add id="item_number" to your input, then the code should work. You also need to move your script to after the DOM element. Otherwise, it will run before the input exists in the document.

Update

Just noticed another mistake. You're setting a Value property, and Request.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 is value (lowercase). There is no equivalent of Request.QueryString in JavaScript. Rather, to extract query string values, see this answer for a good way to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文