我有 2 个 jQuery 脚本 - 一个在我添加 .preventDefault 之前,在我添加 .preventDefault 之后有一个相同脚本的副本。jQuery 在初始时有效,但在我添加 .preventDefault() 后无效
有效的初始脚本
$(window).load(function(){
$(document).ready(function(){
$("span[id$='_ff5_1']").each(function() { //returns a collection of elements that must be iterated through using each
if ($(this).text() == "Yes") { //test value returned from non-input field
clearID();
$("tr.anon").hide();
} else {
$("tr.anon").show();
}
});
if ($("select[title='action']").val() == "") {
$("tr.actDet").hide();
}
$("select[title='organizationalElement']").focusout(function() {
if ($(this).val() === "I don\'t know") {
alert("If no organizational element is selected, additional time may be required to route this request");
} // close if
$("select[title='action']").change(function(){
$(".actDet").toggle($(this).val()!= "");
}); // close action.change
});//close select.focusout
}); // close doc.ready
}); // close window.load
脚本不起作用...
$(window).load(function(){
$(document).ready(function(){
$("span[id$='_ff5_1']").each(function() { //returns a collection of elements that must be iterated through using each
if ($(this).text() == "Yes") { //test value returned from non-input field
clearID();
$("tr.anon").hide();
} else {
$("tr.anon").show();
}
});
if ($("select[title='action']").val() == "") {
$("tr.actDet").hide();
}
$("select[title='organizationalElement']").focusout(function() {
if ($(this).val() !== "I don\'t know") {
$(this).preventDefault();
} else {
alert("If no organizational element is selected, additional time may be required to route this request");
} // close if
$("select[title='action']").change(function(){
$(".actDet").toggle($(this).val()!= "");
}); // close action.change
});//close select.focusout-- close edit record stuff
}); // close doc.ready
}); // close window.load
我所做的唯一更改是最初的 if 语句变成了调用 .preventDefault() 的 if/else。第一个脚本效果很好,但第二个脚本失败了。为什么?如果organizationElement 的值是现有记录上的idk,我将调用.preventDefault() 方法。
@Andrew:澄清您的编辑...我应该将我的脚本修改为:...
if ($(this).val() !== "I don\'t know") {
$(this).click( function(e) { e.preventDefault(); } );
} else {
alert("If no organizational element is selected, additional time may be required to route this request");
} // close if
... b/c 我注意到如果我更改 $(this).preventDefault(); 它将正常工作 到 e.preventDefault();
如果我希望将方法附加到 $(this) 对象,就像我最初编写的那样,您是否可能试图展示如何编写它?