我已经用 jQuery 附加了新表单,当我从下拉列表中选择一项时,我需要在 div/表单下方附加它,但它总是附加两次。我不知道为什么!其他 jquery 元素没有添加它,我已经检查过,我在 jQuery 的函数内部使用它
这是我的代码
function addBrand( brandName )
{
jQuery('#example').append('' +
'<form name="search" action="" method="get" id="search">' +
'<div class="add_marke parentElement" id="add_marke_'+brandName+'" > ' +
' <div class="car_tag"><span class="lbl">' +brandName+ ' ' + ' <span id="car_name" class="icon-remove car-remove"></span></span></div> ' + ' ' +
' <input type="hidden" name="markeName" id="markeName" value="'+brandName+'"></input>'+
' <select name="model_cars" id="model_cars_'+brandName+'" class="search_dropdown_small ">' +
' <option id="selectModel" name="selectModel_All" value="all" >Alle </option>' +
' <option id="selectModel_Selected" name="selectModel_Selected" value="selected" >Nur gewählte</option>'+
' <option id="selectModel_Excluded" name="selectModel_Excluded" value="exclude" >Alle außer</option>' +
' </select>'+ ' ' +
'</div>' +
'</form>');
return(this);
}
jQuery("#model").change(function(){
var brandName = jQuery("#model").val();
jQuery(this).ready(addBrand(brandName));
});
最佳答案
应该这样读:
function addBrand( brandName )
{
jQuery('#example').append('' +
'<form name="search" action="" method="get" id="search">' +
'<div class="add_marke parentElement" id="add_marke_'+brandName+'" > ' +
' <div class="car_tag"><span class="lbl">' +brandName+ ' ' + ' <span id="car_name" class="icon-remove car-remove"></span></span></div> ' + ' ' +
' <input type="hidden" name="markeName" id="markeName" value="'+brandName+'"></input>'+
' <select name="model_cars" id="model_cars_'+brandName+'" class="search_dropdown_small ">' +
' <option id="selectModel" name="selectModel_All" value="all" >Alle </option>' +
' <option id="selectModel_Selected" name="selectModel_Selected" value="selected" >Nur gewählte</option>'+
' <option id="selectModel_Excluded" name="selectModel_Excluded" value="exclude" >Alle außer</option>' +
' </select>'+ ' ' +
'</div>' +
'</form>');
return true;
}
jQuery(document).ready(function(){
jQuery("#model").change(function(){
var brandName = jQuery("#model").val();
addBrand(brandName);
});
});
关于javascript - jQuery 总是追加所选项目两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24908547/