使用 jQuery 填写表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8914161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fill in a form with jQuery
提问by nick2k3
I am trying to use jQuery to fill in a form with some default values.
我正在尝试使用 jQuery 来填写具有一些默认值的表单。
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:
该表单包含在具有 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!"/>
<br/>
</div>
I need to set the coordX_0
text field with some value, let's say: 123
.
我需要coordX_0
用一些值设置文本字段,让我们说:123
.
I thought I could do
我以为我可以
$("#coordX_0").text.value = 123;
But it doesn't seem to work. Any hint?
但它似乎不起作用。任何提示?
回答by Adam Rackis
You can use the val()function to set input values
您可以使用val()函数来设置输入值
$("#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:
顺便说一下,您的原始代码可以通过在实际的底层 dom 元素上设置 value 属性来工作。您可以通过索引 jQuery 结果来访问 dom 元素:
$("#coordX_0")[0].value = 123;
回答by Jasper
All the answers are the exact same so I thought I'd post something different:
所有的答案都完全一样,所以我想我会发布一些不同的东西:
var inputs_to_values = {
'coordX_0' : 'some value',
'coordY_0' : 'some other value',
'edit_0' : 'N',
'remove_0' : '_',
'add_0' : '-',
'go_0' : 'stop?'
};
$('#form_coord_0').find('input').val(function (index, value) {
return inputs_to_values[this.id];
});
You can pass .val()
a function, whatever is returned from the function for each element will be the new value:
您可以传递.val()
一个函数,该函数为每个元素返回的任何内容都将是新值:
A function returning the value to set.
thisis the current element.
Receives the index position of the element in the set and the old value as arguments.
返回要设置的值的函数。
这是当前元素。
接收集合中元素的索引位置和旧值作为参数。
Source: http://api.jquery.com/val
The above code expects that each input will have a property in the inputs_to_values
object so you can convert the ID of the input to the new value for that input.
上面的代码期望每个输入在inputs_to_values
对象中都有一个属性,因此您可以将输入的 ID 转换为该输入的新值。
Here is a demo: http://jsfiddle.net/zYfpE/
这是一个演示:http: //jsfiddle.net/zYfpE/
回答by Pastor Bones
To set the value of an input field its
设置输入字段的值
$("#coordX_0").val(123)
回答by Tom
$("#coordX_0").val("123");
You need the quotes too I think. Although it might work both ways.
我认为你也需要引号。虽然它可能双向工作。
回答by Matt Fenwick
回答by Lai Yu-Hsuan
Just write:
写就好了:
$("#coordX_0").val(123);
In jQuery
, text
is a function to extract text in dom.
In jQuery
,text
是提取dom中文本的函数。
回答by Jason
use the val function
使用 val 函数
$("#coordX_0").val("123");
回答by ShankarSangoli
Use val
method to set the text of any input
field.
使用val
方法设置任何input
字段的文本。
$("#coordX_0").val(123);
回答by Kyle Trauberman
Use this instead:
改用这个:
$("#coordX_0").val("123");