我正在尝试使用 jQuery 来填写带有一些默认值的表单。
该表单包含在具有 id 的 div 中。事实上,每个元素都有一个 id,我这样做只是为了能够使用 $("#id")
语法快速选择表单的每个部分。
表格如下:
<div id="panel" style="position: absolute; left: 190px; top: 300px; width: 400px; height: 300px; border: medium groove brown; background: none repeat scroll 0% 0% black; z-index: 100; color: white;">
<form id="form_coord_0">
X <input type="text" style="height: 25px; font-size: 10px;" size="2" name="X" id="coordX_0"/>
Y <input type="text" style="height: 25px; font-size: 10px;" size="2" name="Y" id="coordY_0"/>
<input type="button" id="edit_0" value="M"/>
<input type="button" id="remove_0" value="-"/>
<input type="button" id="add_0" value="+"/>
<input type="button" id="go_0" value="go!"/>
</form>
</div>
我需要为 coordX_0
文本字段设置一些值,比方说:123
。
我以为我能做到
$("#coordX_0").text.value = 123;
但是好像不行。有什么提示吗?
最佳答案
您可以使用val()设置输入值的函数
$("#coordX_0").val(123);
顺便说一句,您的原始代码可以通过在实际的底层 dom 元素上设置 value 属性来工作。您可以通过索引 jQuery 结果来访问 dom 元素:
$("#coordX_0")[0].value = 123;
关于jquery - 使用 jQuery 填写表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914161/