简体   繁体   English

如何获得触发功能的事件?

[英]How to get event that has fired the function?

Suppose that I've this: 假设我有这个:

<form onsubmit="Popup(this)">

when I press the submit button the Popup function is fired. 当我按下submit按钮时, Popup功能被触发。 How can I get the submit event and prevent it? 如何获取并阻止submit事件?

Popup: function(item)

Thanks. 谢谢。

try this: 尝试这个:

function( item ) {
  alert( "Handler for .submit() called." );
  item.preventDefault();
  // rest of the code
}

OR 要么

    $("#form_id").validate({
        submitHandler: function (form) {
            // do code
            return false;
    }})

Since you tagged your question with jQuery. 由于您使用jQuery标记了问题。

$('form').submit(Popup); //this inside the function is the form.
                         // The first parameter is the event.

And make sure to return false from Popup 并确保从Popup返回false

If Popup isn't a constructor, use camelCase naming 如果Popup不是构造函数,请使用camelCase命名

Your existing code should work if you just modify it just a bit. 如果您只需稍作修改,您的现有代码就可以使用。 Add return to your Popup method call return添加到您的Popup方法调用中

<form onsubmit="return Popup(this)">

And at the end of your method declaration add return false 在方法声明的末尾添加return false

Popup: function(item){
           // your code goes here
           return false; //add this
       }

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

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