Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

jquery - How to remove the selected element during a clone() operation

I have a select box that gets cloned. I want to remove the user's previous selection from each cloned select box. Here is the method that does the clone() :

function addselect(s){
   $('#product_categories > .category_block:last').after(
      $('#product_categories > .category_block:last').clone()
   );
   set_add_delete_links();
   return false;
}

function set_add_delete_links(){
    $('.remove_cat').show();
    $('.add_cat').hide();
    $('.add_cat:last').show();
    $("#product_categories > .category_block:only-child > .remove_cat").hide();
}

function removeselect(s){
    $(s).parent().remove(); 
    set_add_delete_links(); 
    return false;
}

This kinda works but doesn't remove the last selected:

 $('#product_categories > .category_block:last option:selected').remove(); 

Here is the HTML

<div id="product_categories">
<div class="category_block">
    <select name="category_ids[]" id="cat_list">
        <option  value="">Select Attribute</option>
        <option  value="1770">Foo0</option>
        <option  value="1773">Foo1</option>
        <option  value="1775">Foo2</option>
        <option  value="1765">Foo3</option>
        <option  value="1802">Foo4</option>
        <option  value="1766">Foo5</option>
    </select>
    <input class="specsinput" type="text" name="specs[]" value="" />
    <a href="#" onClick="return removeselect(this);" class="remove_cat"> [-] </a>
    <a href="#" onClick="return addselect(this);" class="add_cat"> [+] </a>
</div>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to clone the proper element, set the selected value for the clone, then append the clone.

function addselect(s){

        // Store the block in a variable
    var $block = $('#product_categories > .category_block:last');

        // Grab the selected value
    var theValue = $block.find(':selected').val();

        // Clone the block 
    var $clone = $block.clone();

        // Find the selected value in the clone, and remove
    $clone.find('option[value=' + theValue + ']').remove();

        // Append the clone
    $block.after($clone); 

    set_add_delete_links(); return false;
}

UPDATE: Modified to fit HTML added to question.

Please note, the ID of the select element is being cloned, which means you have 2 elements with the same ID. This is not allowed. You'll need to get rid of the ID, or change it in the clone to some other value.

You could do something like this before you append the clone:

    // Grab the select in the clone
var $select = $clone.find('select');

    // Update its ID by concatenating theValue to the current ID
$select.attr('id', $select.attr('id') + theValue);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...