本文介绍了在表单中传递javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我是Javascript的新手,从我读过的内容中我认为有一种更好的方法可以实现我的方法,但就这样了。 我有一个onclick表单,当在表格单元格中单击清晰图像时,单元格的背景图片会更改(显示已选中,或者如果再次单击则未选中)。一切正常,现在我想定义一些变量,以便当按下表单的提交按钮时,会传递一个表单变量,显示用户是否选择了该选项。 我在互联网上找到的功能性javascript: var curPic1 = 0 ; window .onload = function (){ document .getElementById(' clear-kayangan')。onclick = function (){ curPic1 =(curPic1 == 0 )? 1 : 0 ; document .getElementById(' td_kayangan').style.backgroundImage =(curPic1 == 1 )? ' url(pics / map-kayangan-lake-selected.png)':' url(pics / map-kayangan-lake.png)'; } } 我的计划是撰写一份IF声明,说明curPic1 == 1(背景)选择的图像显示用户已选择),我将创建一个全局变量,然后我可以将其传递给php: $ spot1 = < script>文档。写(spot1);< /脚本>中; if ($ spot1 == 1) echo <输入名称='spot1'类型='隐藏'值='1'/>; 出于测试目的,我试过: $ spot1 = $ _POST [' spot1']; if ($ spot1 == 1 ) echo 这真的有效< br>; 如果我使用If $ spot1 == 0,在我的javascript函数的顶部我尝试了以下: spot1 = 0 ; var spot1 = 0 ; 我在javascript函数中尝试过: spot1 = 1 ; window .spot1 = 1 ; 我还没有尝试过IF语句,因为我只是想测试它们是否正常工作。我读到我应该能够在函数内建立一个全局变量。我一次添加了一个以上的各种选项。它适用于spot = 0,所以我假设问题是它没有被设置为函数中的全局变量?或者也许有更好的方法来解决这一切? On提交隐藏的表单变量最终会传递到另一个页面并在那里使用。 我尝试了什么: 花了几个小时试图从其他例子中学习,但我想我对于javascipt来说太新了。阅读一些教程。谢谢你的帮助。解决方案 spot1 = <脚本>文件撰写(spot1);< /脚本> 中; if ( spot1 == 1) echo <输入名称='spot1'类型='隐藏'值='1'/>; 出于测试目的,我试过: spot1 = I'm a newbie to Javascript and from what I've read I assume there is a much better way to my approach, but here it goes.I have a form with onclick such that when a clear image is clicked on in a table cell, the background pic of the cell changes (showing that it has been selected, or unselected if clicked again). That all works, now I want to define some variable so that when the form's Submit button is pressed, a form variable is passed showing whether or not the user selected that option.The functional javascript I found on the internet:var curPic1 = 0;window.onload=function() { document.getElementById('clear-kayangan').onclick=function() { curPic1 = (curPic1 == 0)? 1 : 0; document.getElementById('td_kayangan').style.backgroundImage = (curPic1 == 1)? 'url("pics/map-kayangan-lake-selected.png")' : 'url("pics/map-kayangan-lake.png")'; }}My plan was to write an IF statement stating if curPic1 == 1 (the background image chosen shows that the user has selected), I would create a Global variable which I could then pass on to php in the form:$spot1 = "<script>document.write(spot1);</script>";if ($spot1 == 1) echo "<input name='spot1' type='hidden' value='1' />";For testing purposes I tried:$spot1=$_POST['spot1'];if ($spot1=="1") echo "This really works<br>";and it works if I use If $spot1==0, whereby at the top of my javascript function I tried the following:spot1=0;var spot1=0;and within the javascript function itself I tried:spot1 = 1;window.spot1 = 1;I haven't yet tried the IF statement because I'm just trying to test things if they work. I read that I was supposed to be able to establish a Global variable from within a function. I added the various options above one at a time. It works with spot=0, so I assume the problem is that it is not being set as a Global variable within the function? Or perhaps there is a better approach to all this?On Submit the hidden form variables will eventually be passed to another page and used there.What I have tried:Spent hours trying to learn from other examples but I guess I'm just too new at javascipt. Read some tutorials as well. Thank you for your help. 解决方案 spot1 = "<script>document.write(spot1);</script>";if (spot1 == 1) echo "<input name='spot1' type='hidden' value='1' />";For testing purposes I tried:spot1= 这篇关于在表单中传递javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-22 03:36