用jQuery填写表单 [英] Fill in a form with jQuery
问题描述
我试图用jQuery来填充一些默认值的表单。
表单包含在一个具有id的div中。实际上每个元素都有一个id,我只是为了能够使用 $(#id)
语法快速选择每一个表单。
以下是表格:
< div id =panelstyle =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 =textstyle =height:25px; font-size:10px; size =2name =Xid =coordX_0/>
Y < input type =buttonid =edit_0value =M/>
< input type =buttonid =remove_0value = - />
< input type =buttonid =add_0value =+/>
< input type =buttonid =go_0value =go!/>
< br />
< / div>
我需要设置 coordX_0
文本字段有一些价值,让我们说: 123
。
我以为我可以做
$ $ $ $ $#coordX_0 ).text.value = 123;
但它似乎不起作用。任何提示?
您可以使用 val()函数设置输入值
$(#coordX_0)。 VAL(123);顺便说一下,你的原始代码可以通过在实际的基础dom元素上设置value属性来工作。您可以通过索引jQuery结果来访问dom元素:
$(#coordX_0)[0] .value = 123;
I am trying to use jQuery to fill in a form with some default values.
The form is contained in a div which has an id. In fact every element has an id, I did so just to be able to quickly select every piece of the form using $("#id")
syntax.
Here is the form:
<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!"/>
<br/>
</div>
I need to set the coordX_0
text field with some value, let's say: 123
.
I thought I could do
$("#coordX_0").text.value = 123;
But it doesn't seem to work. Any hint?
解决方案 You can use the val() function to set input values
$("#coordX_0").val(123);
Incidentally, your original code could be made to work by setting value property on the actual, underlying dom element. You access the dom element by indexing your jQuery results:
$("#coordX_0")[0].value = 123;
这篇关于用jQuery填写表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!