使用jQuery向输入字段添加值 [英] Adding value to input field with jQuery
本文介绍了使用jQuery向输入字段添加值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用jQuery在输入字段中添加一些值.问题在于输入字段的ID.我正在使用ID,例如options[input2]
.在这种情况下,我的代码不起作用.如果我使用input2
之类的ID,则可以正常工作.我需要使用options[input2]
,如何修复代码?
I want to add some value in an input field with jQuery. The problem is with the ID of the input field. I am using the id such as options[input2]
. In that case my code does not work. If I use ID like input2
, then it works fine. I need to use options[input2]
, how can I fix the code?
HTML:
<div>
<h3>Working</h3>
<input id="input1" type="text" value="" />
<input class="button" type="button" value="Add value" />
</div>
<div>
<h3>Not working</h3>
<input id="options[input2]" type="text" value="" />
<input class="button" type="button" value="Add value" />
</div>
jQuery:
$('.button').click(function(){
var fieldID = $(this).prev().attr("id");
$('#' + fieldID).val("hello world");
});
演示:
推荐答案
您可以按照以下步骤进行操作.
You can do it as below.
$(this).prev('input').val("hello world");
这篇关于使用jQuery向输入字段添加值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文