使用 jquery 将某些表单输入设置为某些值
如何循环遍历具有特定名称的所有表单输入并将其设置为指定值?
我尝试了这个 jsfiddle 但无法让它工作。 http://jsfiddle.net/qvcA6/
$(document).ready(function() {
$("#link-1").click(function() {
$('[name=price]','#myform').val('0.00');
});
});
编辑:我认为这并不重要,但是数组不考虑表单字段名称的键。因此,您的所有示例都有效,但是当我将键放入字段名称中时,它们就会中断。有人知道如何处理钥匙吗?更新了 jsfiddle -> http://jsfiddle.net/qvcA6/8/
How do i cycle through all form inputs with a certain name and set those to a specified value?
I tried this jsfiddle and i can't get it to work. http://jsfiddle.net/qvcA6/
$(document).ready(function() {
$("#link-1").click(function() {
$('[name=price]','#myform').val('0.00');
});
});
EDIT: i didn't think it would matter, but the array key for the form field name isn't accounted for. So all of your examples are working but they break when i put keys in the field names. Anybody know how to go about accounting for the keys? updated jsfiddle -> http://jsfiddle.net/qvcA6/8/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为您更新了 jfiddle http://jsfiddle.net/qvcA6/1/
有两个问题。
1) 属性值需要用引号引起来。
2)您输入的名称是“price[]”而不是“price”,因此jquery选择器不匹配任何内容
Updated the jfiddle for you http://jsfiddle.net/qvcA6/1/
There were two issues.
1) The attribute value needs to be surrounded by quotes.
2) The name of your inputs were 'price[]' and not 'price' so the jquery selector didn't match anything
这是一个 小提琴
Here's a fiddle
您的输入名称是
price[]
而不是price
,而且在使用属性 equals 选择器时指定标签名称会更有效,特别是在长格式中。这是您修改后的小提琴的链接 http://jsfiddle.net/qvcA6/7/
The name of your inputs is
price[]
notprice
, also it would be more efficient expecially in a long form to specify the tag name when using the attribute equals selector.Here is a link your modified fiddle http://jsfiddle.net/qvcA6/7/
请参阅我的 jsFiddle:
See my jsFiddle: