2014-11-03 99 views
2

我想一个用户已经从下拉列表我的一个选项后,创建一个元素。不幸的是我没有得到我乐意要的结果。我已经取得的结果是这样的:试图追加一个元素的jQuery

Faulty result

正如你所看到的链接“删除”已经由用户做出选择的每个选项后面被递增。在创建的div和锚元素之间也没有空间。我想我需要一个for循环来修复增量链接remove。但我真的不知道如何。我曾尝试以下:

$("#theSelect").change(function() { 
 
    var value = $("#theSelect option:selected").val(); 
 
    var theDiv = $(".is" + value); 
 

 
    //theDiv.slideDown().removeClass("hidden"); 
 
    $("#theSelect option:selected").attr('disabled', 'disabled'); 
 

 
    var $div = $("<div>", { 
 
    id: "foo", 
 
    class: "a", 
 
    text: value 
 
    }); 
 
    //$div.click(function(){ /* ... */ }); 
 
    $(".selectContainer").append($div); 
 

 
    var newLink = $("<a />", { 
 
    id: "id5", 
 
    name: "link", 
 
    href: "#", 
 
    text: "remove" 
 
    }); 
 

 
    $(".a").append(newLink); 
 

 
}); 
 

 
$("div a.remove").click(function() { 
 
    var value = $(this).attr('rel'); 
 
    var theDiv = $(".is" + value); 
 

 
    $("#theSelect option[value=" + value + "]").removeAttr('disabled'); 
 

 
    $(this).parent().slideUp(function() { 
 
    $(this).addClass("hidden"); 
 
    }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="selectContainer"> 
 
    <select id="theSelect"> 
 
    <option value="">- Select -</option> 
 
    <option value="Patient">Patient</option> 
 
    <option value="Physician">Physician</option> 
 
    <option value="Nurse">Nurse</option> 
 
    </select> 
 
</div> 
 
<!-- <div class="hidden isPatient">Patient <a href="#" class="remove" rel="Patient">remove</a></div> --> 
 
<!-- <div class="hidden isPhysician">Physician <a href="#" class="remove" rel="Physician">remove</a></div> --> 
 
<!-- <div class="hidden isNurse">Nurse <a href="#" class="remove" rel="Nurse">remove</a></div> -->

回答

3

重要的错误是,你附加的删除链接到所有div.a元素

只是其附加到新创建的一个(你必须把它参考)的

相反

$(".a").append(newLink);  

使用

$new.append(newLink);  

此外,您应该缓存,如果你不止一次地访问它们使用的元素引用。使用本机性能也优选提供

时,那么这个

var value = $("#theSelect option:selected").val(); 
var theDiv = $(".is" + value); 

//theDiv.slideDown().removeClass("hidden"); 
$("#theSelect option:selected").attr('disabled','disabled'); 

可以成为

var option = this.options[this.selectedIndex], 
    value = option.value, 
    theDiv = $(".is" + value); 

option.disabled = true; 

末,

  • 你可以在创建时删除处理程序绑定到元素..
  • 你还需要设置rel属性
  • 你需要删除的元素,而不是只隐藏它,因为你创建新的每次改变select元素
  • 需要,因为你创建删除id5具有相同ID的多个链接和无效

所有更改一起

$("#theSelect").change(function(){   
    var option = this.options[this.selectedIndex], 
     value = option.value; 

    var theDiv = $(".is" + value); 

    //theDiv.slideDown().removeClass("hidden"); 
    option.disabled = true; 

    var $div = $("<div>", {id: "foo", class: "a", text: value }); 
    //$div.click(function(){ /* ... */ }); 
    $(".selectContainer").append($div); 

    var newLink = $("<a />", { 
     name : "link", 
     href : "#", 
     text : "remove", 
     rel : value, 
     click: remove 
    }); 

    $div.append(newLink);  

}); 

function remove() { 
    var value = $(this).attr('rel'); 
    var theDiv = $(".is" + value); 

    $("#theSelect option[value=" + value + "]").removeAttr('disabled'); 

    $(this).parent().slideUp(function() { $(this).remove(); }); 
}; 

在演示http://jsfiddle.net/gaby/pzq8hqjw/7/

+0

嗨,谢谢你的回答。但说实话,我不明白你的第一点,“你可以在创建时绑定删除处理程序的元素。”你的意思是我创建一个div元素,我也创建一个锚元素?如果是这样,你或其他人可能会展示如何完成?我还在Google上搜索过有关rel属性的内容。 rel属性只是为了更好的SEO吗?还是有更多的我可能不知道呢?我理解你的最后两点:)。 – superkytoz 2014-11-04 00:32:41

+1

@superkytoz我忘了***到***。 “*你可以在创建时绑定移除处理程序**到元素*。我的意思是你试图添加删除处理程序到链接的方式不起作用(*当你的$(因此,通过将'click:remove'作为var newLink = $(“”,{..})的选项之一传递给'var a.remove' )'代码,你可以设置处理器在链接被点击时使用。在我的代码中,我已经将你的代码放入函数中,并将它传递给链接。 – 2014-11-04 00:44:09