首页 > javascript > Javascript输出为隐藏输入的值

Javascript输出为隐藏输入的值 (Javascript output as value of hidden input)

问题

如何将此javascript函数的输出设置为html表单上隐藏输入的值?

document.write(states[i][1]);

工作正常,但我无法用代码填写值,如下所示。

    if (to == 'abbr'){
        input = input.replace(/\w\S*/g, function(txt){return 
        txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
        for(i = 0; i < states.length; i++){
            if(states[i][0] == input){
            document.getElementById("sid").value = (states[i][1]);
                 }
             }    
         } 
     }       

</script>
<form action="we2.php" method="post">
<input type="text" id="sid" name="s1"/>
<input type="submit" value="Verify">
</form>

这段代码出了什么问题?这样做的正确方法是什么?

谢谢!

解决方法

通过将整个函数转换为变量然后将变量转换为DOM,我找到了一个更简单的解决方案:

var response = abbrState('<?php echo $_GET['state']; ?>', 'abbr');

document.getElementById("sid").value = response;

问题

How can I set the ouput of this javascript function as the value for a hidden input on a html form?

document.write(states[i][1]);

works fine but I cannot get it to fill in the value with the code as shown below.

    if (to == 'abbr'){
        input = input.replace(/\w\S*/g, function(txt){return 
        txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
        for(i = 0; i < states.length; i++){
            if(states[i][0] == input){
            document.getElementById("sid").value = (states[i][1]);
                 }
             }    
         } 
     }       

</script>
<form action="we2.php" method="post">
<input type="text" id="sid" name="s1"/>
<input type="submit" value="Verify">
</form>

What is wrong with this code / what is the right way to do this?

Thanks!

解决方法

I found a easier solution by just turning the entire function into a variable then the variable into the DOM:

var response = abbrState('<?php echo $_GET['state']; ?>', 'abbr');

document.getElementById("sid").value = response;
相似信息