0

我尝试为克隆的复选框设置唯一的名称,而不是获取,让我们说:

 Origin_1, Origin_2, Origin_3, for cloned 1, 2, 3 of the same element,

我得到:

Origin_1, Orignin_11, Origin_111

我理解这个问题:克隆使用过去的克隆作为原始名称。我不知道应该采取什么方法。要么找到使用原始名称的方法,要么将前一个克隆的名称修剪一个字符,并确保计数器不会再次从头开始。您的提示将不胜感激。

这是javascript代码:

$(window).load(function() { 
    var bindFunction = $('#quickmenuall input:checkbox').change(function(){ 
        var uniqueId = 1;
        var currentName = $(this).attr('name'); 
        newName = currentName + (uniqueId++);
        if($(this).is(':checked')){
            var clonedClient = $(this).parent().parent().parent().parent().parent().clone(true) 
            var ori = $(this).parent().parent().parent().parent().parent() 
            clonedClient.insertBefore(ori)
            $(this).attr('name', newName)
            var clonedClient = $(this);
            $(this).prop('checked', false)
        } else {
            var clonedClient = $(this);
            clonedClient.parent().parent().parent().parent().parent().remove() 
            checkbox.bind("change", bindFunction);
            bindFunction();
        }
    });
});

这是一个jsfiddle:http: //jsfiddle.net/Sergelie/ULywc/

4

3 回答 3

0

部分问题是您的 uniqueId 在更改函数的范围内声明,因此始终以 1 开头。此外,您正在使用后增量运算符 (uniqueId++),在将其分配给 newName 后将其更改为 2。这就是你得到“1”、“11”、“111”等的原因。尝试将 uniqueId 移动到 window.load 中(例如)。

尝试这个:

$(window).load(function() { 
        var uniqueId = 1;

    var bindFunction = $('#quickmenuall input:checkbox').change(function(){ 
        var currentName = $(this).attr('name'); 
        newName = currentName + (uniqueId++);
        if($(this).is(':checked')){
            var clonedClient = $(this).parent().parent().parent().parent().parent().clone(true) 
            var ori = $(this).parent().parent().parent().parent().parent() 
            clonedClient.insertBefore(ori)
            $(this).attr('name', newName)
            var clonedClient = $(this);
            $(this).prop('checked', false)
        } else {
            var clonedClient = $(this);
            clonedClient.parent().parent().parent().parent().parent().remove() 
            checkbox.bind("change", bindFunction);
            bindFunction();
        }
    });
});

并将其与还建议的正则表达式方法结合使用,或者简单地为您的复选框保存一个“前缀”ID。

var prefix = 'checkbox_';
var uniqueId = 1;
var id1 = prefix + (uniqueId++).toString(); // checkbox_1
var id2 = prefix + (uniqueId++).toString(); // checkbox_2
var id3 = prefix + (uniqueId++).toString(); // checkbox_3
于 2014-03-11T01:50:53.323 回答
0

您将需要使用 regex 或 substring() 来获取原始名称

我已经设法使它与子字符串一起使用,因为我知道我正在构建我的克隆,例如 One_1 One_2 等......但是,最好使用正则表达式来完成它,例如在@cbayram 的另一个答案中。当我有时间时,我将构建一个正则表达式并更新小提琴。

var lastIndexOf_ = oldName.lastIndexOf('_');
// we build the clones with and underscore + number. get substring from the beginning of     the oldName until the "_"
if (lastIndexOf_ != -1)
    oldName = oldName.substring(0,lastIndexOf_);
// else just use the oldName
var newName = oldName + '_' + i++; // build new name and increment counter.

请检查这个 jsfiddle http://jsfiddle.net/houssamk/Dzr58/39/

我还重新设计并清理了事件处理程序的绑定方式。我使用 $.on() 而不是 bind()。$.on() 更好,因为它在创建新克隆时动态附加事件处理程序。

希望能帮助到你

于 2014-03-11T14:39:02.123 回答
0

currentName是一个字符串,你正在将一个整数连接到它。

newName = "1"+1; // "11"
newName = 1+1; // 2

要解决您的直接问题,请使用正则表达式,匹配 的数字/数字部分currentName并将其解析为整数,然后再将您的uniqueId添加到它。

// Regex goes something like
// starts with a non-digit character 1 or more times 
// followed by and ending with zero or more digits
var nameParts = currentName.match(/^([^0-9]+)(\d*)$/);
newName = nameParts[1] + (parseInt(nameParts[2]||0) + uniqueId++);

小提琴叉

更新

看看这个小提琴

阅读下面的评论以获得解释。我相信你不再需要 uniqueId 了。

input[name^="F_1_"]获取所有克隆(以 F_1_ 开头)input[name="F_1"],获取原始

var currentName = $(this).attr('name'); 
// We are again matching the core part of the clone element to get the itemFamily
var nameParts = currentName.match(/^([^0-9]+(_[0-9]+))(_[0-9]+)?$/);
// Then we use the attribute starts with selector and attribute equals to selector
// to get the original and all clones of with the core itemFamily name part
// the length attribute will be your next number in the item family clone.
var itemFamilyCount = $('input[name^="'+nameParts[1]+'_"], input[name="'+nameParts[1]+'"]').length;
newName = nameParts[1] + '_' + itemFamilyCount;
于 2014-03-11T01:30:47.913 回答