【发布时间】:2014-08-30 14:16:32
【问题描述】:
我需要在选择时将 div 元素附加到另一个 div,我已经做到了,而且每次我选择新的选择时,它都应该附加新的 div。
问题是,那些附加的 div 有相同的 id,所以我不能删除特定的元素。我通过声明 var 并将 id 与字符串组合使它们独一无二,例如:id="add_marke'+selectedKey+'" 但我不能这样做jQuery( "#add_marke'+selectedKey+'" ).click(function() {
制作唯一ID时如何选择ID?
var selectedKey = jQuery("#model").val();
jQuery('#example').append('' + '<div class="add_marke" id="add_marke' +
selectedKey + '" >' +
' <div class="car_tag"><span class="lbl"><span id="car_name" class="icon-remove"></span></div> ' +
' ' +
' <select name="model_cars" id="model_cars" ><option id="selectModel" name="selectModel" value="all" >Please select </option><option>test</option></select>' +
' ' + '</div>');
jQuery("#car_name").click(function() {
jQuery("#add_marke\\.selectedKey").remove();
});
【问题讨论】:
-
不要定义点击 id 而是整个类并在点击事件中检查 id。 $(document).on('.add_marker', function(e) {...});
-
您可以使用 jquery 最接近选择器。为按钮提供类名并为主 div 提供类名。现在做这件事。 $('.buttonSelector').on('click',function(e){ $(this).closet('.parentDiv').remove()})
标签: javascript jquery