简体   繁体   English

使用jQuery在输入字段中添加多个值

[英]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/

You add strings together with + 您将字符串与+一起添加

$("button").on('click', function(e) {
    e.preventDefault();
    var elem = $(this).parent().find('input[name=myinput\\[\\]]');

    elem.val( elem.val() + 'add this' );
});

FIDDLE 小提琴

Now you only need something useful to add ? 现在您只需要添加一些有用的东西?

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+");

});

DEMO FIDDLE 演示场

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM