如何在Javascript中将事件对象传递给函数?

[英]How to pass an event object to a function in Javascript?


function check_me() {
  //event.preventDefault();
  var hello = document.myForm.username.value;
  var err = '';

  if(hello == '' || hello == null) {
    err = 'User name required';
  }

  if(err != '') { 
     alert(err); 
     $('username').focus(); 
     return false; 
   } else { 
    return true; }
}

In Firefox, when I try to submit an empty value it throws up the error and sets the focus back to element. But same thing doesn't happen in IE as it throws up error and after clicking OK and posts the form (returns true).

在Firefox中,当我尝试提交一个空值时,它会抛出错误并将焦点设置回元素。但同样的事情在IE中不会发生,因为它会引发错误并在单击“确定”并发布表单后返回(返回true)。

How I can avoid this? I was thinking to avoid this using event.preventDefault(), but I am not sure how to do this using this method. I tried passing checkme(event) .. but it didnt work. I am using Prototype js.

我怎么能避免这个?我正在考虑使用event.preventDefault()避免这种情况,但我不知道如何使用此方法执行此操作。我试过通过checkme(事件)..但它没有工作。我正在使用Prototype js。

(I know how to pass an event when I bind an .click function in javascript.. instead of calling onclick within html .. using Jquery, but I have to debug this piece of code)

(我知道如何在javascript中绑定.click函数时传递一个事件..而不是在html中调用onclick ...使用Jquery,但我必须调试这段代码)

3 个解决方案

#1


21  

Try making the onclick js use 'return' to ensure the desired return value gets used...

尝试使onclick js使用'return'来确保使用所需的返回值...

<button type="button" value="click me" onclick="return check_me();" />

#2


140  

  1. Modify the definition of the function check_me as::

    修改函数check_me的定义为::

    function check_me(ev) {
    
  2. Now you can access the methods and parameters of the event, in your case:

    现在您可以访问事件的方法和参数,在您的情况下:

    ev.preventDefault();
    
  3. Then, you have to pass the parameter on the onclick in the inline call::

    然后,您必须在内联调用的onclick上传递参数::

    <button type="button" onclick="check_me(event);">Click Me!</button>
    

A useful link to understand this.

了解这一点的有用链接。


Full example:

完整示例:

<!DOCTYPE html>
<html lang="en">
  <head>
    <script type="text/javascript">
      function check_me(ev) {
        ev.preventDefault();
        alert("Hello World!")
      }
    </script>
  </head>
  <body>
    <button type="button" onclick="check_me(event);">Click Me!</button>
  </body>
</html>

#3


-4  

I would change your binding to be:

我会改变你的绑定:

<button type="button" value="click me" onclick="check_me" />

I would then change your check_me() function declaration to be:

然后我会将check_me()函数声明更改为:

function check_me() {   
  //event.preventDefault();
  var hello = document.myForm.username.value;
  var err = '';

  if(hello == '' || hello == null) {
    err = 'User name required';
  }

  if(err != '') { 
     alert(err); 
     $('username').focus(); 
     event.preventDefault(); 
   } else { 
    return true; }
}

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2009/08/14/c41bbfdabfb5faaa291a74f1e225f739.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号