我有一个2 jQuery的scripts-一个之前我加.preventDefault,和相同的脚本的副本后,我加入了.preventDefault。 jQuery的工作在最初,但不是在我加入.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。第一个脚本很好,但第二个脚本失败。为什么?如果organizationalElement的值是现有记录上的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我注意到taht它会正常工作,如果我改变$(本).preventDefault();到e.preventDefault();
你也许想显示怎么写,如果我想的方法连接到$(本)对象作为我最初写呢?
感谢亚当。我知道preventDefault不会阻止事件冒泡,但我不确定我不想冒泡。这可能阻止了整个jQuery脚本的运行? – jg100309 2012-01-04 16:54:55
@ jg100309 - 不,你原来的问题是你在这个上调用preventDefault,而不是事件对象。将事件对象传递给你的处理程序,并在* it *上调用preventDefault,就像在我的回答中一样 – 2012-01-04 16:58:25
*注意:如果在两个's之间,你不需要转义'''* – SeinopSys 2013-03-01 13:57:15