gpt4 book ai didi

javascript - 将变量从一个 HTML 页面传递到另一个页面的最佳做法是什么?

转载 作者:太空狗 更新时间:2023-10-29 14:54:33 26 4
gpt4 key购买 nike

我是 Web 应用程序编程的新手,所以我希望这个问题对每个人来说都不是太基础。

我创建了一个带有 FORM 的 HTML 页面,其中包含一个 dojox 数据网格 (v1.2),其中填充了不同杂货商品的描述行。用户选择了他感兴趣的项目后,他将点击“提交”按钮。

此时,我可以获得将项目 ID 号存储为 javascript 变量的 javascript 函数,但我不知道如何将此 ID 传递到后续 HTML 页面。

我应该只将 ID 作为 URL 查询字符串参数传递吗?还有其他更好的方法吗?

编辑:整个过程就像一个购物车。用户将从网格中选择项目,然后在下一页上用户将填写一些详细信息,然后结账。

我还应该提到我正在使用 grails,所以这是在 GSP 页面中发生的,但目前它只包含 HTML。

最佳答案

您可以只使用一个隐藏的输入字段;作为表单的一部分传输。

<html>
<head>
</head>
<body>
<script type="text/javascript">
function updateSelectedItemId() {
document.myForm.selectedItemId.value = 2;
alert(document.myForm.selectedItemId.value);

// For you this would place the selected item id in the hidden
// field in stead of 2, and submit the form in stead of alert
}
</script>

Your grid comes here; it need not be in the form

<form name="myForm">
<input type="hidden" name="selectedItemId" value="XXX">
The submit button must be in the form.
<input type="button" value="changeSelectedItem" onClick="updateSelectedItemId()">
</form>
</body>
</html>

关于javascript - 将变量从一个 HTML 页面传递到另一个页面的最佳做法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/216536/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com