Javascript 将javascript变量值传递给输入类型隐藏值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7764154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass a javascript variable value into input type hidden value
提问by KaHeL
I would like to assign value of product of two integer numbers into a hidden field already in the html document. I was thinking about getting the value of a javascript variable and then passing it on a input type hidden. I'm having a hard time to explain but this is how it should work:
我想将两个整数的乘积值分配到 html 文档中已有的隐藏字段中。我正在考虑获取 javascript 变量的值,然后将其传递给隐藏的输入类型。我很难解释,但这就是它应该如何工作:
Script Example
脚本示例
<script type="text/javascript">
function product(a,b){
return a*b;
}
</script>
above computes the product and i want the product to be in hidden field.
以上计算产品,我希望产品在隐藏字段中。
<input type="hidden" value="[return value from product function]">
How is this possible?
这怎么可能?
回答by Darin Dimitrov
You could give your hidden field an id:
你可以给你的隐藏字段一个 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.
确保在 DOM 完全加载后执行此分配,例如在window.load
事件中。
回答by gion_13
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));
回答by Jarrett Widman
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">
回答by 151291
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>
回答by collyg
Check out this jQuery page for some interesting examples of how to play with the value attribute, and how to callit:
查看此 jQuery 页面,了解有关如何使用 value 属性以及如何调用它的一些有趣示例:
Otherwise - if you want to use jQuery rather than javascript in passing variables to an input of any kind, use the following to setthe value of the input on an event click()
, submit()
et al:
否则-如果你想使用jQuery,而不是JavaScript中传递变量到任何类型的输入,使用以下方法来设置一个事件的输入值click()
,submit()
等:
on some event; assign or setthe 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.
使用这种方法,显然要确保 html 输入尚未指定值或禁用属性。
Beware the differences betwen .html()
and .text()
when dealing with html forms.
请注意处理 html 表单时.html()
和之间的差异.text()
。
回答by nitin
<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">
回答by simoncereska
add some id for an input
为输入添加一些 id
var multi = product(2,3);
document.getElementById('id').value=multi;
回答by william dunlap
//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...
现在这令人困惑,但这应该有效......