[英]onchange event for select tag using jquery
I have a jQuery,where I am appending select tag inside a table for each row, I want to keep onChange
event for each dropdown on every row. 我有一个jQuery,我要在表格内为每行附加选择标记,我想为每行的每个下拉列表保留onChange
事件。 I tried using onchange
event as shown, but not working. 我尝试使用所示的onchange
事件,但无法正常工作。 My jQuery code is as below: 我的jQuery代码是如下:
for (var j = 0; j < leng; j++) {
o = proplist[j];
txt = "<tr><td>" + o.Value + "</td><td><select name=" + o.Key + "onchange='OnChange();'>";
for (var k = 0; k < data.length; k++) {
if (data[k] == o.Value || data[k] == o.Key) {
txt1 += " <option selected>" + data[k] + "</option>";
} else txt1 += " <option>" + data[k] + "</option>";
}
txt = txt + txt1 + "<option value='0'>Ignore</option></select></td></tr>";
$("#propertycolumnListTable tbody").append(txt);
txt = "";
txt1 = "";
}
You can try this: 您可以尝试以下方法:
$('select').change(function () {
OnChange();
});
Try this 尝试这个
$(function() {
$("#propertycolumnListTable tbody").on("change","select",function() {
// whatever you want when any select in the tbody changes
});
});
and if you have jQuery, why not use it? 如果您有jQuery,为什么不使用它呢?
for (var j = 0; j < leng; j++) {
o = proplist[j];
var $tr = $("<tr/>");
var $td = $("<td/>");
var $sel = $("<select/>",{name:o.Key});
$sel.append('<option value='0'>Ignore</option>');
var selected = null;
data.each(function() {
$sel.append("<option/>",{value:this,text:this});
if (this == o.Value || this == o.Key) selected=this;
});
if (selected) $sel.val(selected);
$td.append($sel);
$tr.append($td);
$("#propertycolumnListTable tbody").append($tr);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.