[英]Add multiple values in input field with jQuery
I want to add multiple input values in an input field with jQuery. 我想使用jQuery在输入字段中添加多个输入值。 So that everytime I hit the button, a new value is added in the same field along with the old value.
这样,每当我按下按钮时,都会在旧字段中添加一个新值。
I am trying following code, but it does not add the value, it simply overwrites the previous value. 我正在尝试遵循以下代码,但它没有添加值,它只是覆盖了先前的值。
HTML: HTML:
<div class="wrap">
<button>Add value</button>
<input name="myinput[]" value="" />
</div>
jQuery: jQuery的:
$("button").click(function(e) {
e.preventDefault();
$(this).parent().find('input[name=myinput\\[\\]]').val("value+");
});
Demo: http://jsfiddle.net/D97bV/ 演示: http : //jsfiddle.net/D97bV/
Try: 尝试:
$("button").click(function(e) {
e.preventDefault();
var val = $(this).parent().find('input[name=myinput\\[\\]]').val();
$(this).parent().find('input[name=myinput\\[\\]]').val(val+"value+");
});
try this: 尝试这个:
$("button").click(function(e) {
e.preventDefault();
var myInput = $(this).parent().find('input[name=myinput\\[\\]]');
myInput.val(myInput.val() + "value+");
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.