如何获取已触发该功能的事件?

时间:2016-08-14 12:25:49

标签: javascript jquery

假设我是这样的:

<?php

    function Yearsbox($echo=false) {
        $output = "";
        for ($i=2000; $i < 2020 ; $i++) {
            $output .=$i;
        }
        if($echo){
            echo $output;
            return null;
        }
        return $output;
    }

    echo Yearsbox();

当我按下<form onsubmit="Popup(this)"> 按钮时,submit功能被触发。如何获取Popup事件并阻止它?

submit

感谢。

3 个答案:

答案 0 :(得分:0)

试试这个:

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

OR

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

答案 1 :(得分:0)

因为你用jQuery标记了你的问题。

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

确保从Popup

返回false

如果Popup不是构造函数,请使用camelCase命名

答案 2 :(得分:0)

如果您只修改一下,现有代码应该可以正常工作。将return添加到您的Popup方法调用

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

在方法声明结束时添加return false

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