我有一个表,在这个表中,每一行都是使用ajax生成的。 在每一行中都有一个带id的select标签(selectTax)。 我想创建一个选择此选择时发生的js函数。 为此,我正在使用
$("#selectTax").change(function(){
console.log("vikash");
});
但是当我点击这个没什么事情发生的时候。 请尽快帮帮我。
答案 0 :(得分:0)
您的代码存在两个问题:
。 HTML中的标识符必须是唯一的。因此,请改用class
选择器。
。对于动态html,请使用$(document).on()
,如:
$(document).on('click', '.selector', function(){
// your logic here
});
答案 1 :(得分:0)
尝试这个我还没有添加ajax代码,但与您的代码类似,我生成了动态选择标记并添加了其更改事件。
检查您的控制台是否有错误
function AddSelect(){
var select = $("<select class='content-select'><option>1</option><option>2</option></select>");
$("#content").append(select);
}
$(document).on('change', '.content-select', function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="button" value="Add Select tag" onclick="AddSelect()">
<div id="content">
</div>