Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

javascript - 将javascript变量值传递给输入类型隐藏值(Pass a javascript variable value into input type hidden value)

I would like to assign value of product of two integer numbers into a hidden field already in the html document.(我想将两个整数的乘积值分配给html文档中已有的隐藏字段。)

I was thinking about getting the value of a javascript variable and then passing it on a input type hidden.(我正在考虑获取javascript变量的值,然后将其传递给隐藏的输入类型。) I'm having a hard time to explain but this is how it should work:(我很难解释,但这是它应该如何工作:) 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?(这怎么可能?)   ask by KaHeL translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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事件中。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...