我刚刚得到的信息是我的jQuery函数无法在IE或Edge上运行。在控制台中,我收到消息:
对象不支持“最近”的属性或方法
这是jQuery:
$('body').on('change', 'select', function (event) {
if(event.target.id.indexOf("couche") >= 0) {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: event.target.id,
value: event.target.value,
iscouche: "True"
},
}).done(function (msg) {
if(msg.nothing == 1) {
var what = event.target.closest('tbody');
$(what).find("tr:gt(0)").remove();
} else {
var add = event.target.closest('tr');
var toremove = msg.toremove.split(" ");
for(var i = 0; i < toremove.length; i++) {
if(toremove[i].length > 0) {
jQuery(toremove[i]).remove();
}
}
jQuery(add).after(msg.ret);
}
});
} else {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: event.target.id,
value: event.target.value,
iscouche: "False"
},
}).done(function (msg) {});
}
});
有人可以告诉我是否有解决办法?
event.target
是DOM节点,而不是jQuery对象,因此没有jQuery方法
在jQuery中,请改用$(this)
jQuery对象。
我还建议您在不需要时不要使用目标。
更新:较新的浏览器现在具有最接近的DOM方法,因此OPs代码将在除IE之外的较新的浏览器中工作。
这是固定的jQuery版本:
$('body').on('change', 'select', function(event) {
var $sel = $(this), // the changed select
id = this.id, // or $(this).attr("id");
val = $(this).val(); // or this.value
if (id.indexOf("couche") >= 0) {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: id,
value: val,
iscouche: "True"
},
}).done(function(msg) {
if (msg.nothing == 1) {
var what = $sel.closest('tbody')
$(what).find("tr:gt(0)").remove();
} else {
var add = $sel.closest('tr');
var toremove = msg.toremove.split(" ")
for (var i = 0; i < toremove.length; i++) {
if (toremove[i].length > 0) {
jQuery(toremove[i]).remove();
}
}
jQuery(add).after(msg.ret);
}
});
} else {
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: id,
value: val;,
iscouche: "False"
},
}).done(function(msg) {});
}
});
或更整洁:
$('body').on('change', 'select', function(event) {
var $sel = $(this), // the select changed
val = this.value,
id = this.id,
isCouche = id.indexOf("couche") != -1;
$.ajax({
url: "{{ redir2 }}",
type: "POST",
data: {
ident: id,
value: val,
iscouche: isCouche ? "True" : "False";
},
}).done(function(msg) {
if (isCouche) {
if (msg.nothing == 1) {
var what = $sel.closest('tbody')
$(what).find("tr:gt(0)").remove();
} else {
var add = $sel.closest('tr');
var toremove = msg.toremove.split(" ")
for (var i = 0; i < toremove.length; i++) {
if (toremove[i].length > 0) {
$(toremove[i]).remove();
}
}
$(add).after(msg.ret);
}
} else {
// handle not couche
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句