简体   繁体   English

无法将隐藏的表单值获取到javascript变量中

[英]Unable to get hidden form value into javascript variable

I have a form with several fields. 我有几个领域的表格。 The value of one of the hidden fields is passed in the URL along with some other input values like this: 隐藏字段之一的值与其他一些输入值一起传递到URL中,如下所示:

example.html#firstname=Homer&lastname=Simpson&itype=TARGET example.html的#姓名=荷马&姓=辛普森&ITYPE = TARGET

I have a javascript that parses the URL and that is populating the form just fine. 我有一个解析URL的JavaScript,并且可以很好地填充表单。 The output of the form contains all of the values passed from the url, so I know the variable are getting passed to the form fields. 表单的输出包含从url传递的所有值,因此我知道变量已传递到表单字段。

My issue is - I want to be able to setup an if statement to change the text of one of the headings based on the value of itype. 我的问题是-我希望能够设置一个if语句,以根据itype的值更改标题之一的文本。

Here's my code: 这是我的代码:

        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");
        }

I added the first document.write statement as a debug and it's coming back with: The value of the variable is undefined 我添加了第一个document.write语句作为调试,然后返回: 变量的值未定义

Here's what the itype field variable looks like: 这是itype字段变量的样子:

    <input type="hidden" name="itype" id="itype" value="">

Any ideas how I can get the variable from the URL into my javascript variable so I can do my if statement? 有什么想法可以将变量从URL转换为javascript变量,以便执行if语句吗?

Change : 变更

 document.write ("<p>The value of the variable is " + itypestuff.value + "</p>");

To

document.write ("<p>The value of the variable is " + itypestuff + "</p>");

OR 要么

Change 更改

var itypestuff = document.getElementById("itype").value;

To

var itypestuff = document.getElementById("itype");
// And 
if( itypestuff.value == "TARGET" ){ //...

EDIT 编辑

You have to change your html like that : 您必须像这样更改html:

<input type="hidden" name="itype" id="itype" value="<?= htmlspecialchars($_GET['itype']); ?>">

And call : example.php?firstname=Homer&lastname=Simpson&itype=TARGET 并致电:example.php?firstname = Homer&lastname = Simpson&itype = TARGET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM