简体   繁体   English

从HTML表单字段调用JavaScript函数

[英]calling JavaScript functions from HTML form fields

When calling a JavaScript function what does the return value do to an input form field? 调用JavaScript函数时,返回值对输入表单字段有什么作用? For example: 例如:

<script type="text/javascript">
 function foo()
 {
  return true;
 }
</script>
<input type="text" id="txt1" onkeyup="return foo()" />

and variations, IIRC onkeyup="foo(); return false" 和变体,IIRC onkeyup="foo(); return false"
I don't really need a return time, what's the easiest way to do this? 我真的不需要返回时间,最简单的方法是什么?

Right now I'm trying to call multiple functions from one event and not sure what the appropriate way is: <input type="text" id="txt1" onkeyup="return foo1(); return foo2();" 现在,我正在尝试从一个事件调用多个函数,并且不确定合适的方式是: <input type="text" id="txt1" onkeyup="return foo1(); return foo2();" or 要么
<input type="text" id="txt1" onkeyup="return foo1()" onkeyup="return foo2();"

return false; is used to stop the event from having its default effect (in this case, the letter typed from being added). 用于停止事件的默认效果(在这种情况下,不会添加键入的字母)。 return foo() is used so that foo can be the judge of whether to return true or return false and therefore whether or not the default action should happen. return foo()用于使foo可以判断是return true还是return false以及是否应该执行默认操作。

If you are just calling a function that won't affect whether or not the event actually takes place, just use onkeyup="foo1(); foo2();" 如果您只是调用一个不会影响事件是否实际发生的函数,则只需使用onkeyup="foo1(); foo2();" . If, however, one of them affects the outcome, it should be called last and have a return before it. 但是,如果其中之一影响结果,则应在最后调用它,并在return之前return

In general, though, it's easier to have a single function in there, say onkeyup="return foo()" , and then have 不过,总的来说,在其中拥有一个函数会比较容易,比如说onkeyup="return foo()" ,然后

function foo() {
    foo1();
    foo2();
    return true; // or whatever
}

The most appropriate way is to bind multiple functions to one event like so: 最合适的方法是将多个函数绑定到一个事件,如下所示:

ELEMENT.addEventListener('onkeyup', function(event) {
    foo();
    bar();
    fooBar();
}, false);

Or to simplify it even further you could use jQuery: 为了进一步简化它,您可以使用jQuery:

$(element).on({
    keyup: function() {
        foo();
        bar();
        fooBar();
    }
});

*Hint: use this (in jquery) or event.target (non-jquery) to work with the event source. *提示:使用this (在jquery中)或event.target (非jquery)来处理事件源。

Ideally you shouldnt be putting them inline AT ALL... you should be using event handlers attached to the dom elements. 理想情况下,您根本不应该将它们内联...您应该使用附加到dom元素上的事件处理程序。

for example: 例如:

document.getElementById('txt1').addEventListener('onkeyup', function() {
   foo1();
   foo2();
});

Now the caveat here is that event assignment isnt necessarily normalized so you need to attach listeners in different ways in different browsers. 现在需要注意的是,事件分配不一定要规范化,因此您需要在不同的浏览器中以不同的方式附加侦听器。 This is one of the reasons people use libraries like jQuery - it normalizes all this. 这是人们使用jQuery之类的库的原因之一-标准化了所有这些。

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

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