将变量传递给表单

时间:2014-02-24 07:07:20

标签: javascript html html5

如何将javascript中的变量(imageS)传递给我的HTML表单的隐藏字段?

<div class="popup">
    <form action="myphp.php" method="POST">
        <label for="name">Name</label>
        <input type="text" name="name"><br /><br />
        <input type="hidden" name="location">
        <input type="submit" name="submit" value="Submit">
    </form>
</div>

JS

var imageX = 'www.myweb.com/images/'+image+'.jpg';

5 个答案:

答案 0 :(得分:1)

document.forms[0].location.value = imageX;

答案 1 :(得分:0)

拥有隐藏输入的ID,例如id="imagePath"

 <input type="hidden" name="location" id="imagePath">

现在使用如下的javascript,

var imageX = 'www.myweb.com/images/'+image+'.jpg';
document.getElementById("imagePath").value = imageX;

答案 2 :(得分:0)

给你一个id属性隐藏字段...

<input type="hidden" name="location" id="location">

然后使用javascript将该属性的值设置为imageX值。

var imageX = 'www.myweb.com/images/'+image+'.jpg';
document.getElementById("location").value=imageX;

答案 3 :(得分:0)

您可以使用:

document.forms[0].elements["location"].value = imageX;

答案 4 :(得分:0)

你可以附加onclick事件并调用js函数为隐藏输入赋值

HTML:

<div class="popup">
        <form action="myphp.php" method="POST">
        <label for="name">Name</label>
        <input type="text" name="name"><br /><br />
        <input type="hidden" name="location">
        <input type="submit" name="submit" value="Submit" onclick='passImg();'>
       </form>
       </div>

JS:

function passImg(){
var imageX = 'www.myweb.com/images/'+image+'.jpg';
var a= $('[name="location"]').val(imageX);
}
相关问题