gpt4 book ai didi

jQuery .preventDefault();

转载 作者:行者123 更新时间:2023-12-03 22:02:21 27 4
gpt4 key购买 nike

我有 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) 对象,您是否可能试图展示如何编写它?

最佳答案

您想要在事件对象上调用 preventDefault,而不是 this

$("select[title='organizationalElement']").focusout(function(e) {          
if ($(this).val() !== "I don\'t know") {
e.preventDefault();
}
});
<小时/>

为了完整起见,请注意 preventDefault 会阻止此元素的默认操作 - 例如,将页面导航到 anchor 的 href 属性值(我我不确定选择焦点的默认操作是什么,或者是否有一个)。 preventDefault 防止冒泡。

如果您碰巧关心冒泡(我并不是说您一定应该关心),从 jQuery 事件处理程序返回 false 既会阻止默认操作,也会阻止冒泡。

关于jQuery .preventDefault();,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8730505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com