将 javascript 变量值传递到输入类型隐藏值

我希望将两个整数乘积的值赋给 html 文档中已经存在的一个隐藏字段。 我在考虑获取一个 javascript 变量的值,然后将它传递给一个隐藏的输入类型。 我很难解释清楚,但事情应该是这样的:

脚本示例

 <script type="text/javascript">
function product(a,b){
return a*b;
}
</script>

上面计算的产品,我希望产品是在隐藏领域。

<input type="hidden" value="[return value from product function]">

这怎么可能?

462081 次浏览

You could give your hidden field an id:

<input type="hidden" id="myField" value="" />

and then when you want to assign its value:

document.getElementById('myField').value = product(2, 3);

Make sure that you are performing this assignment after the DOM has been fully loaded, for example in the window.load event.

add some id for an input

var multi = product(2,3);
document.getElementById('id').value=multi;

if you already have that hidden input :

function product(a, b) {
return a * b;
}
function setInputValue(input_id, val) {
document.getElementById(input_id).setAttribute('value', val);
}

if not, you can create one, add it to the body and then set it's value :

function addInput(val) {
var input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('value', val);
document.body.appendChild(input);
}

And then you can use(depending on the case) :

addInput(product(2, 3)); // if you want to create the input
// or
setInputValue('input_id', product(2, 3));

You could do that like this:

<script type="text/javascript">
function product(a,b)
{
return a*b;
}
document.getElementById('myvalue').value = product(a,b);
</script>


<input type="hidden" value="THE OUTPUT OF PRODUCT FUNCTION" id="myvalue">
<script type="text/javascript">
function product(x,y)
{
return x*y;
}
document.getElementById('myvalue').value = product(x,y);
</script>


<input type="hidden" value="THE OUTPUT OF PRODUCT FUNCTION" id="myvalue">

Hidden Field :

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

Script :

<script type="text/javascript">
var year = new Date();
document.getElementById("year").value=(year.getFullYear());
</script>

Check out this jQuery page for some interesting examples of how to play with the value attribute, and how to call it:

http://api.jquery.com/val/

Otherwise - if you want to use jQuery rather than javascript in passing variables to an input of any kind, use the following to set the value of the input on an event click(), submit() et al:

on some event; assign or set the value of the input:

$('#inputid').val($('#idB').text());

where:

<input id = "inputid" type = "hidden" />


<div id = "idB">This text will be passed to the input</div>

Using such an approach, make sure the html input does not already specify a value, or a disabled attribute, obviously.

Beware the differences betwen .html() and .text() when dealing with html forms.

//prompts for input in javascript
test=prompt("Enter a value?","some string");


//passes javascript to a hidden field.
document.getElementById('myField').value = test;


//prompts for input in javascript
test2=prompt("Enter a value?","some string2");


//passes javascript to a hidden field
document.getElementById('myField').value = test2;


//prints output
document.write("hello, "+test+test2;

now this is confusing but this should work...