本文介绍了删除输入后,将输入名称元素数组中的数字从零开始排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在jQuery代码更改为name元素的html代码中删除其中一行(例如:第2行)之后.

I want after remove one of the rows (Ex: Row 2) in html code changed to in name element by jQuery.

例如,我们首先在html中具有以下内容:

For example we first have in html as:

现在,如果我们删除第二行,实际上,在删除了第二行之后,我们将得到如下行:

Now if we remove the second row, In fact, after removed the row 2, we have rows like this:

但是我想要它的结果(删除前两行之一)后为:

But i want it in result (after remove one of rows ex: row 2) as:

我尝试使用(请参见我的完整代码),但不起作用: http://jsfiddle.net/k3wne/

I tried as (see my full code) but Don't work: http://jsfiddle.net/k3wne/

Js:

$('.remove_input').live('click', function (e) {
    e.preventDefault();
    var remove = $(this).closest($class);
    remove.fadeOut('slow', function () {
        $('.add_units').each(function (idx, val) {
            var num = $('.add_units').length;
            NumIdx = (num - (num - idx));
            //for(var i = 0; i < num-1; i++){
            $(this).closest($class_guide).next($class_guide).each(function (idx, val) {
                $('.add_units input[type="checkbox"]').attr('name', function (idx, str) {
                    var int = parseInt(str.match(/\d+/)[0], 10);
                    return str.replace(int, NumIdx);
                })
            });
            //}
        })
    });)
}​

推荐答案

此处

DEMO

$('.remove_input').live('click', function (e) {
    e.preventDefault();
    var remove = $(this).closest('.RowCheck');
    remove.fadeOut('slow', function () {
        $(this).remove(); // or change next line to  $('.RowCheck:visible')
        $('.RowCheck').each(function (idx) {
            var checkBoxes = $('input[type="checkbox"]',this);
            checkBoxes.each(function(i) {
              var str = $(this).attr('name');  
              var currentIdx = parseInt(str.match(/\d+/)[0], 10);  
              $(this).attr('name', str.replace(currentIdx,idx));
           })
        });
    });
});​

如果您愿意的话,此代码将起作用

This code will work if you either

  1. 实际删除该行-爆炸药
  2. 也建议
  3. 像Simon一样测试可见性
  1. remove the row for real - also suggested by Explosion Pills or
  2. test for visible like Simon did

这篇关于删除输入后,将输入名称元素数组中的数字从零开始排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 11:33