简体   繁体   English

通过增加部分价值

[英]Replace part of value by incrementing it

I have a set of inputs which can be duplicated, but I then need to increment the array value of them. 我有一组可以重复的输入,但是然后我需要增加它们的数组值。 How do I do this? 我该怎么做呢?

<input type="text" name="person[0][name]">
<input type="text" name="person[0][age]">

This can then be duplicated, but I need to change the new inputs to be like: 然后可以将其复制,但是我需要将新输入更改为:

<input type="text" name="person[1][name]">
<input type="text" name="person[1][age]">

I have got so far: 我到目前为止:

cloned.find('input, select').each(function(){
    var $this = $(this);

    $this.attr('name',
        $this.attr('name').match(/\[\d+]/g, '[' + what_goes_here + ']')
    );
});

Where cloned is my last group of inputs. 我的最后一组输入是在哪里cloned的。

What do I do with the value of what_goes_here ? 我用what_goes_here的值怎么办?

You can use .match() with RegExp \\d+ , + operator, .replace() 您可以将.match()RegExp \\d++运算符, .replace()

cloned.find('input, select').each(function() {
    var $this = $(this);
    var n = $this.attr('name').match(/\d+/)[0];
    $this.attr('name', $this.attr('name').replace(n, +n + 1));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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