简体   繁体   English

jQuery .preventDefault();

[英]jQuery .preventDefault();

I have a 2 jQuery scripts- one before I added .preventDefault, and a copy of the same script after I added the .preventDefault. 我有2个jQuery脚本-在添加.preventDefault之前的一个,和在添加.preventDefault之后的相同脚本的一个副本。 jQuery works in the initial, but not after I add .preventDefault() jQuery最初可以使用,但是我添加.preventDefault()之后不能使用

Initial script that works 初始脚本有效

$(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 

Script that does not work... 脚本不起作用...

$(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 

The only change I made is the initial if statement becomes an if/else that calls .preventDefault(). 我所做的唯一更改是将初始if语句变为调用.preventDefault()的if / else。 The first script works great, but the second script fails. 第一个脚本运行良好,但是第二个脚本失败。 Why? 为什么? I'm calling the .preventDefault() method if the value of the organizationalElement is idk on an existing record. 如果organizationalElement的值在现有记录上为idk,我将调用.preventDefault()方法。

@Andrew: To clarify on your edit... Should I revise my script to: ... @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 I noted taht it will work correctly if I change $(this).preventDefault(); ... b / c我注意到,如果我更改$(this).preventDefault();它将正常工作。 to e.preventDefault(); 到e.preventDefault();

Are you perhaps trying to show how to write it if I wish to attach the method to the $(this) object as I'd originally written it? 如果我希望像我最初编写的那样将方法附加到$(this)对象上,您是否正在尝试显示如何编写它?

You want to call preventDefault on the event object, not this 您要在事件对象上调用preventDefault ,而不是this函数

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

Just for completeness, note that preventDefault prevents the default action of this element—navigating the page to the value of the href attribute for an anchor, for example (I'm not sure what the default action is for a select's focusout, or if there even is one). 仅出于完整性考虑,请注意preventDefault阻止该元素的默认操作 -例如,将页面导航到锚的href属性值(例如,我不确定用于select的焦点输出的默认操作是什么,或者甚至是一个)。 preventDefault does not prevent bubbling. preventDefault 不会阻止冒泡。

If you happen to care about bubbling—and I'm not saying that you necessarily should—returning false from a jQuery event handler will both prevent the default action, and also prevent bubbling. 如果您碰巧关心冒泡(我并不是说您必须这样做),则从jQuery事件处理程序返回false既可以防止默认操作,又可以防止冒泡。

The preventDefault() function is associated with the event. preventDefault()函数与事件关联。 What you should be calling is: 您应该拨打的电话是:

e.preventDefault();

edit to clarify based on your comment, you need to add e as a variable in the function call: 编辑以根据您的注释进行澄清,您需要在函数调用中将e添加为变量:

$('selector').click( function(e) { e.preventDefault(); } );

You can read more about in on the jQuery preventDefault page. 您可以在jQuery preventDefault页面上阅读有关in的更多信息。

The preventDefault method should be applied to the event object, not to the DOM object as you're doing it. preventDefault方法应应用于事件对象,而不是应用于DOM对象。

Your code should be: 您的代码应为:

$("select[title='organizationalElement']").focusout(function(e) {          
       if ($(this).val() !== "I don\'t know") {
         e.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

Let me know if that helps! 让我知道是否有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM