简体   繁体   English

提交时使用JavaScript的Opera问题

[英]Opera problem with javascript on submit

I have an input element with onchange="do_something()". 我有一个带有onchange =“ do_something()”的输入元素。 When I am typing and hit the enter key it executes correctly (do_something first, submit then) on Firefox and Chromium (not tested in IE and Safari), however in Opera it doesn't (it submits immediately). 当我键入并按下Enter键时,它可以在Firefox和Chromium(未在IE和Safari中进行测试)正确执行(先执行do_something,然后提交),但是在Opera中却无法执行(立即提交)。 I tried using a delay like this: 我尝试使用这样的延迟:

<form action="." method="POST" onsubmit="wait_using_a_big_loop()">
<input type="text" onchange="do_something()">
</form>

but it didn't work either. 但它也不起作用。

Do you have some recommendations? 你有什么建议吗?

Edit: Finally I used a mix of the solutions provided by iftrue and crescentfresh, just unfocus the field to fire do_something() method, I did this because some other input fields had others methods onchange. 编辑:最后,我混合使用了iftrue和crescentfresh提供的解决方案,只是使该字段不集中以触发do_something()方法,我这样做是因为其他一些输入字段具有onchange的其他方法。

$('#myForm').submit( function(){
    $('#id_submit').focus();
} );

Thanks 谢谢

You could use jquery and hijack the form. 您可以使用jquery并劫持表单。

http://docs.jquery.com/Events/submit http://docs.jquery.com/Events/submit

<form id = "myForm">

</form>

   $('#myForm').submit( function(){
   do_something();
   } );

This should submit the form after calling that method. 这应该在调用该方法后提交表单。 If you need more fine-grained control, throw a return false at the end of the submit event and make your own post request with $.post(); 如果您需要更细粒度的控制,请在Submit事件结束时抛出return false,并使用$ .post()发出自己的发布请求。

From http://cross-browser.com/forums/viewtopic.php?id=123 : 来自http://cross-browser.com/forums/viewtopic.php?id=123

Per the spec, pressing enter is not supposed to fire the change event. 根据规范,按Enter键不会触发更改事件。 The change event occurs when a control loses focus and its value has changed. 当控件失去焦点并且其值已更改时,将发生更改事件。

In IE pressing enter focuses the submit button - so the text input loses focus and this causes a change event. 在IE中,按Enter键会聚焦“提交”按钮-因此文本输入会失去焦点,从而导致更改事件。

In FF pressing enter does not focus the submit button, yet the change event still occurs. 在FF中,按Enter键不会使“提交”按钮聚焦,但是更改事件仍然发生。

In Opera neither of the above occurs. 在Opera中,以上两种情况均不会发生。

keydown is more consistent across browsers for detecting changes in a field. 跨浏览器使用keydown可以更一致地检测字段中的更改。

I'm not great at Javascript, but could you do something like this? 我不太擅长Javascript,但是您可以做这样的事情吗?

<form name="myform" action="." method="POST">
<input type="text" onchange="do_something();document.myform.submit();">
</form>

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

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