1

我有一个动态复制的字段。一切正常,除了我似乎无法更改输入字段的类和父跨度。

标记:

<td class="small-text">
<span class="wpcf7-form-control-wrap submitted-file">
    <input type="file" name="submitted-file-1" value="1" size="40" class="wpcf7-form-control wpcf7-file submitted-file" id="submitted-file-1">
</span>
</td>

(编辑我:这不是完整的标记,只是相关的部分。整个重复的字段是一个完整的表格行<tr>,其 id#o99_the_work可以从脚本中理解..)

JS:

jQuery("#add_row").click(function() {
    var row = jQuery("#o99_the_work tbody > tr:last"),
        newRow = row.clone(true);

    newRow.find("input[type=text],input[type=file]").each(function() {
        var me = jQuery(this).parent();
        var me2 = jQuery(this);
        var num = +(this.id.match(/\d+$/) || [0])[0] + 1;
        this.id = this.id.replace(/\d+$/, "") + num;
        this.name = this.id;
        me2.attr("class",me2.attr("class").replace(/\d+/, function(val) { return parseInt(val)+1; }));
    });

    newRow.insertAfter(row);

    return false;
});

所以有了这个脚本,idname都正确递增。

那么问题出在哪里呢?我还需要submitted-file在输入字段和父跨度上增加类属性,但是这一行:

me2.attr("class",me2.attr("class").replace(/\d+/, function(val) { return parseInt(val)+1; }));

实际上捕获了第一个数字,在我的情况下是wpcf7-form-control,并且它将这个值增加到 wpcf8-form-controlwpcf9-form-control..(这对我来说是错误的)

如何仅增加具有的值submitted-file[-something]

4

1 回答 1

1

问题在于您的正则表达式replace()方法 - 您应该搜索最后一次出现的数字。

me2.attr("class",me2.attr("class").replace(/\d+$/, function(val) { return parseInt(val)+1; }));
于 2013-09-07T07:17:13.467 回答