[英]Assign Value to <input> inside <td> using Javascript
I have an <input type="text" value="0.00" id="prdtotal"/>
inside a <td>
in a table.我在表的
<td>
中有一个<input type="text" value="0.00" id="prdtotal"/>
。
I want to use Javascript to compute a value and assign it to the textbox.我想使用 Javascript 计算一个值并将其分配给文本框。 How can I do it?
我该怎么做?
I tried using document.getElementById("prdtotal").value="1.00";
我尝试使用
document.getElementById("prdtotal").value="1.00";
but it doesnt work.但它不起作用。 Thanks for any help!
谢谢你的帮助!
You've found the element using getElementById
but you've not specified its the value
property you want to set.您已使用
getElementById
找到该元素,但尚未指定要设置的value
属性。
document.getElementById("prdtotal").value="1.00"
And a live example: http://jsfiddle.net/hevwt/还有一个活生生的例子:http: //jsfiddle.net/hevwt/
Use the value attribute:使用值属性:
document.getElementById("prdtotal").value = "1.00";
document.getElementById() returns the actual DOM object (a handle/reference to the <input>
element). document.getElementById()返回实际的 DOM 对象(对
<input>
元素的句柄/引用)。
Also note your missing double quote before the closing parenthesis.还要注意右括号前缺少的双引号。
I would suggest you to take a look at the Mozilla Developer Network (MDN):我建议你看看Mozilla 开发者网络(MDN):
HTMLElement : a reference of all attributes a (general) HTML element can have.HTMLElement :对(一般)HTML 元素可以具有的所有属性的引用。
HTMLInputElement : a reference of all special attributes an input element (eg <textarea>
, <input>
) can have. HTMLInputElement :输入元素(例如
<textarea>
, <input>
)可以具有的所有特殊属性的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.