jQuery追加始终选择的项目两次?

时间:2014-07-23 10:54:44

标签: javascript jquery

我用jQuery添加了新表单,当我从下拉列表中选择一个项目时,我需要在div / form下面附加一个,但它总是追加两次。我不知道为什么!另一个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));
    });

2 个答案:

答案 0 :(得分:1)

应如下所示:

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);
    });
  });

答案 1 :(得分:0)

试试这个

function addBrand( brandName )
    {

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


    }
 $("#model").change(function(){
    var brandName = $("#model").val();
     addBrand(brandName);
});

如果您不想追加,请使用.html(代替.append(

检查 Demo

另一个 DEMO