我有一个包含多个字段的表单。其中一个隐藏字段的值与URL中的一些其他输入值一起传递:
example.html的#姓名=荷马&安培;姓=辛普森&安培; ITYPE = TARGET
我有一个解析URL的javascript,并且正好填充表单。表单的输出包含从url传递的所有值,因此我知道变量将传递给表单字段。
我的问题是 - 我希望能够根据itype的值设置if语句来更改其中一个标题的文本。
这是我的代码:
var itypestuff = document.getElementById("itype").value;
document.write ("<p>The value of the variable is " + itypestuff.value + "</p>");
if( itypestuff == "TARGET" ){
document.write("This is the target");
}
else{
document.write("This is not the target");
}
我添加了第一个document.write语句作为调试,它回来了:变量的值未定义
这里是itype字段变量的样子:
<input type="hidden" name="itype" id="itype" value="">
我有什么想法可以将URL中的变量放到我的javascript变量中,这样我可以做我的if语句吗?
答案 0 :(得分:2)
更改:
document.write ("<p>The value of the variable is " + itypestuff.value + "</p>");
要强>
document.write ("<p>The value of the variable is " + itypestuff + "</p>");
更改
var itypestuff = document.getElementById("itype").value;
致
var itypestuff = document.getElementById("itype");
// And
if( itypestuff.value == "TARGET" ){ //...
你必须改变你的html:
<input type="hidden" name="itype" id="itype" value="<?= htmlspecialchars($_GET['itype']); ?>">
并致电:example.php?firstname = Homer&amp; lastname = Simpson&amp; itype = TARGET