0

複製されたチェックボックスに一意の名前を付けようとしています。取得する代わりに、次のように言いましょう。

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

私は得る:

Origin_1, Orignin_11, Origin_111

私は問題を理解しています: クローンは、過去のクローンを元の名前として使用します。周りを回るアプローチがどうあるべきかわかりません。元の名前を使用する方法を見つけるか、以前のクローンの名前を 1 文字削除して、カウンターが最初から開始されないようにします。あなたのヒントをいただければ幸いです。

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

正規表現または substring() を使用して元の名前を取得する必要があります

One_1 One_2 などのクローンを作成していることを知っているので、部分文字列で機能させることができました。正規表現を作成し、時間があるときにフィドルを更新します。

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/

また、イベント ハンドラーのバインド方法を作り直し、クリーンアップしました。bind() の代わりに $.on() を使用します。$.on() は、新しいクローンが作成されるときにイベント ハンドラーを動的にアタッチするため、はるかに優れています。

それが役に立てば幸い

于 2014-03-11T14:39:02.123 に答える
0

問題の一部は、uniqueId が change 関数のスコープ内で宣言されているため、常に 1 から始まることです。また、newName に割り当てた後に 1 を 2 に変更するポストインクリメント演算子 (uniqueId++) を使用しています。これが、「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

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 に答える