开发者

Jquery change/focus/blur overrides submit in Firefox

开发者 https://www.devze.com 2023-01-22 06:19 出处:网络
A form with a single textfield is given. A submit handler is bound on the form and a change/focus/blur handler bound on the textfield.

A form with a single textfield is given. A submit handler is bound on the form and a change/focus/blur handler bound on the textfield.

When something is entered into the textfield and the submit button is then clicked, only the change/focus/blur event is registered in Firefox, while in Safari, both are registered.

The form:

<form id="form1" action="" method="get">
<input id="text1" name="text1" type="text" />
<input id="submit1" name="submit1" type="submit" />
</form>

The Jquery code:

$(document).ready(function(){
 $('#text1').change(function(){ alert("1"); });
 $('#form1').submit(function(){ alert("2"); });
});

In Firefox: "1" is alerted. In Safari, "1" is al开发者_Go百科erted, then "2" is alerted.

How can I get the behaviour in Safari to work in Firefox?


The problem here looks like it is caused by the alert.

If you run the code without the alert or change it to console.log the code behaves as you expect, with both the change and submit events being fired.

$(function() {

  $('#text1').change(function() {
    console.log("change");
  });

  $('#form1').submit(function() {
    console.log("submit");
    return false;
  });
});

Since you don't ever want to use alert in production code, you should be able to get the result to want without any changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消