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

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

问题描述:

我对网络应用程序编程相对较新,所以我希望这个问题对于每个人来说都不是太基础。

I'm relatively new to web application programming so I hope this question isn't too basic for everyone.

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

I created a HTML page with a FORM containing a dojox datagrid (v1.2) filled with rows of descriptions for different grocery items. After the user selects the item he's interested in, he will click on the "Submit" button.

在这一点上,我可以得到javascript函数将项目ID号存储为javascript变量但是我不知道如何将此ID传递到后续的HTML

At this point, I can get the javascript function to store the item ID number as a javascript variable BUT I don't know how to pass this ID onto the subsequent HTML page.

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

Should I just pass the ID as an URL query string parameter? Are there any other better ways?

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

The overall process is like a shopping cart. The user will select the item from the grid and then on the next page the user will fill out some details and then checkout.

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

I should also mention that I'm using grails so this is happening in a GSP page but currently it only contains HTML.

你可以使用隐藏的输入字段;作为表单的一部分发送。

You could just use a hidden input field; that gets transmitted as part of the form.

<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>