通过表单传递 JS 变量

Passing a JS variable through the form

本文关键字:JS 变量 表单      更新时间:2023-09-26

在下面的form中,我需要document.result[0].value作为表单变量传递给onclick的值到下一页吗?具体而言,此document.result[0].value=totalquestions-incorrect.length+ value需要作为表单数据在store_result.cfm页面上访问。

<form method="POST"><div
align="center"><center><p>
<script>
  var wrong=0
   for (e=0;e<=2;e++)
document.result[e].value=""
var results=document.cookie.split(";")
 for (n=0;n<=results.length-1;n++){
  if (results[n].charAt(1)=='q')
 parse=n
 }
 var incorrect=results[parse].split("=")
 incorrect=incorrect[1].split("/")
 if (incorrect[incorrect.length-1]=='b') 
 incorrect=""
   document.result[0].value=totalquestions-incorrect.length+" out of "+totalquestions
   document.result[2].value=(totalquestions-incorrect.length)/totalquestions*100+"%"
 for (temp=0;temp<incorrect.length;temp++)
   document.result[1].value+=incorrect[temp]+", "

</script>
<input type="button" value="Continue" name="B1"
  onClick="location.href='store_result.cfm';""></p>
 </center></div>
 </form>
 // html
<input id='res' type='hidden' value=''>
 <script>
  var result = 0
   // your script here 
   // calculate the value of result as you wish
  $('#res').val(result);
 </script>

html:

<input type="button" value="Continue" name="B1" onclick="location.href='store_result.cfm?'+document.result[0].value;" />

store_result.cfm:

<script>
    var yourVar = location.href.split('?')[1];
</script>

或者你可以设置饼干。

更改

<input type="button" value="Continue" name="B1" onClick="location.href='store_result.cfm';"">

<input type="button" value="Continue" name="B1" onClick="location.href='store_result.cfm?result=' + escape(document.result[0].value);"">

然后,它将通过下一页的查询字符串提供。