我有html表格
<form name="updatefrm" id="updatefrm" action="" method="post">
<input type="hidden" name="98" id="98" value="" />
<input type="hidden" name="99" id="99" value="" />
<input type="hidden" name="100" id="100" value="" />
<input type="hidden" name="101" id="101" value="" />
<input type="hidden" name="102" id="102" value="" />
<input type="hidden" name="updateqty" id="updateqty" value="1" />
</form>
现在我想为这些元素赋值 我使用以下javascript代码为id为98的元素
document.updatefrm."98".value = elements[0].value;
但它在控制台中给我错误。
任何人都可以帮助我吗?
答案 0 :(得分:1)
Why can't I have a numeric value as the ID of an element?
// Change your numeric id.
var myValue=document.getElementById('your_changed_id').value;
alert(myValue)
简单起见..而不是编写这个复杂的代码。为什么不尝试一个做同样事情的简单代码
document.getElementById('your_changed_id').value
答案 1 :(得分:1)
您应该使用document.formname
来访问表单,因为并非所有浏览器都支持此表单(实际上我不确定是否有)。请改用document.forms.formname
。另外,要访问对象的数字属性,请使用括号表示法而不是点符号。
document.forms['updatefrm']['98'].value = elements[0].value;
答案 2 :(得分:0)
答案 3 :(得分:0)
您也可以使用getElementById
来设置值。除此之外,还要检查Javascript Naming Convention
document.getElementById('98').value = "YOUR VALUE";