将javascript变量值传递给输入类型的隐藏值 [英] Pass a javascript variable value into input type hidden value

查看:187
本文介绍了将javascript变量值传递给输入类型的隐藏值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个整数的乘积值分配到已经在html文档中的隐藏字段。
我正在考虑获取javascript变量的值,然后将其传递给隐藏的输入类型。
我很难解释,但这是应该如何工作的:

脚本示例



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:

 <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?

推荐答案

您可以为您的隐藏字段添加一个id:

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);

确保在DOM完全加载后执行此分配,例如在 window.load 事件。

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

这篇关于将javascript变量值传递给输入类型的隐藏值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆