2016-12-26 28 views
1

我创建了一个动态表,通过点击一个按钮它会在每次按下按钮时生成一个新的行。一切正常,但select标签或任何其他输入中的类属性无法正常工作。我如何才能使其工作。任何想法??使用追加功能js不选择标签阅读的类属性js

这里是.js文件的代码,工作和按一下按钮添加新行:

 $('.plusbtn12').click(function() { 
      $(".quoteTable12").append('<tr><td><select class="js-select2 form-control c_solutions" name="i_solution[]" style="width: 100%;" data-placeholder="Choose one.."></select></td><td class="hidden-xs"><input class="form-control" type="text" name="i_quantity[]" placeholder="Quantity"></td><td><input class="form-control c_costing" type="text" name="i_cost[]" placeholder="Cost"></td></tr>'); 

      var options = ""; 

      $.getJSON("getSolution12.php", function (data) { 

       for (i = 0; i < data.length; i++) { 
        options += "<option>" + data[i] + "</option>"; 
       } 

       $('.quoteTable12 tr:last').find('.c_solutions').html(""); 
       $('.quoteTable12 tr:last').find('.c_solutions').append(options); 
       $(".c_solutions").change(); 

      }); 
     }); 
+0

类? – Nitesh

+0

class =“js-select2” – CoderJoe

+0

您的意思是附加到动态添加元素的事件不起作用? – anu

回答

1

既然你选择添加动态需要了解delegated events

说明:附加基于 特定的一组根元素,为现在或将来与选择器匹配的所有元素的一个或多个事件的处理程序。

选择您选择这样

$(document).on("click", ".js-select2", function() { 
    console.log("hey"); 
}); 
你要哪类添加上选择标签
+0

我在哪里可以实现上面的代码。你可以告诉我,如果可能的话? – CoderJoe

+0

这一点不会解决问题。该事件需要使用'on()'签名正确绑定 – anu

+0

@anu右键。我正要改变它 –