javascript - jQuery 总是追加所选项目两次?

标签 javascript jquery

我已经用 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+ '&nbsp' + '&nbsp;<span id="car_name" class="icon-remove car-remove"></span></span></div> ' + '&nbsp' +
                '   <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&#xdf;er</option>' +
                '   </select>'+ '&nbsp' +

                '</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+ '&nbsp' + '&nbsp;<span id="car_name" class="icon-remove car-remove"></span></span></div> ' + '&nbsp' +
      '   <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&#xdf;er</option>' +
      '   </select>'+ '&nbsp' +

      '</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/

相关文章:

JavaScript/jQuery : How do I remove a range of numbered classes?

javascript - navigator.geolocation 给出错误的结果

javascript - 如何在 Safari 中安装扩展后访问 DOM

javascript - 为什么使用 ReactJS,我无法制作对单击使用react的复选框?

jquery - jquery select2 插件是否支持在下拉列表中具有替代行颜色的功能?

jquery - 如何在 jQuery 中检测 anchor 标记的内容是否包含字符串?

javascript - 将函数声明为变量

javascript - Leaflet map.fitBounds 错误的中心和缩放

javascript - SVG 元素的 Bootstrap 弹出窗口不适用于 jQuery 3.0.0

jquery - WordPress:将内联 JS 移至延迟 jQuery 后触发