简体   繁体   English

尝试附加jQuery元素

[英]Trying to append an element jQuery

I'm trying to create an element after a user has selected an option from my dropdownlist. 用户从我的下拉列表中选择一个选项后,我试图创建一个元素。 Unfortunately I'm not getting the result that I gladly want. 不幸的是我没有得到我想要的结果。 The result that I had achieved looked like this: 我获得的结果如下所示:

错误的结果

As you see the link 'remove' gets incremented after each selected option that has been made by the user. 如您所见,在用户选择了每个选项之后,“删除”链接会增加。 Also there is no space between the created div and anchor element. 在创建的div和anchor元素之间也没有空间。 I think I need a for loop to fix the incrementing link remove . 我想我需要一个for循环来解决递增链接remove But I really don't know how. 但是我真的不知道如何。 I have tried the following: 我尝试了以下方法:

 $("#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> --> 

The important error is that you are appending the remove link to all div.a elements 重要的错误是您div.a remove链接附加到所有div.a元素

Just append it to the newly created one ( you have a reference to it ) 只需将其附加到新创建的( 您对此有一个引用

Instead of 代替

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

use 采用

$new.append(newLink);    

Additionally, you should cache references to elements you use if you access them more than once. 另外,如果您多次访问所使用的元素,则应将其缓存。 Using native properties is also preferred when available 如果可用,也最好使用本机属性

So this 所以这

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

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

can become 可以变成

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

option.disabled = true;

Last, 持续,

  • you can bind the remove handler to the element at creation time.. 您可以在创建时将remove处理程序绑定到该元素。
  • you also need to set the rel attribute 您还需要设置rel属性
  • you need to remove the element instead of just hiding it, since you create new ones each time you change the select element 您需要删除该元素而不是仅将其隐藏,因为每次更改select元素时都会创建一个新元素
  • you need to remove the id5 since you create multiple links with the same id and that is invalid 您需要删除id5因为您创建了多个具有相同id的链接,而这是无效的

All changes together 所有变化在一起

$("#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(); });
};

Demo at http://jsfiddle.net/gaby/pzq8hqjw/7/ 演示位于http://jsfiddle.net/gaby/pzq8hqjw/7/

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

appends newLink to all the matched tags. newLink附加到所有匹配的标签。 Which are in your case, every tag having class a (ie Nurse, Patient). 这是在你的情况下,每一个有标记类a (即护士,病人)。

But what you want to do is to append remove to only the last tag having class a . 但是,您要做的是将remove仅附加到具有a类的最后一个标签上。

Then: 然后:

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

will work for you. 将为您工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM