开发者

Alert in jquery click is not stopping submit

开发者 https://www.devze.com 2023-02-27 02:19 出处:网络
I have <input onclick=\"__doPostBack\"> and $(function(){ $(\'input\').click(function(){ alert(\'hey\') })

I have

<input onclick="__doPostBack">

and

$(function(){ 
    $('input').click(function(){ alert('hey') }) 
});

In Chrome for example I see alert and when click "ok" browser su开发者_高级运维bmits. In FF4 i see on a milisec alert, and browser submits, without waiting for me clicking "ok".

Question: What happened? FF4 has new async events?

That is not only problem of alert, preventDefault is not working also.


I didn't test this, but I think it might work.

$('input').unbind('click').click(function{
    alert('Hey.');
    __doPostBack();
});


Well we were never really supposed to depend on events like alert() or comfirm() to actually stop execution of code, it just so happened that most browsers were doing it that way.

That being said, I don't know why this is happening in FF4, but my guess is that the submit event of the form is actually happening before the click event of the button. To try it out, fly your alert on the submit event of the form and see if that stops submission.

$('input').closest('form').submit(function() {
  alert('Hey');
});


Firefox 4 now uses in page dialog boxes; this reduces the annoyance of modal dialogs in previous versions. I'd assume this is an HTML/JavaScript thing and not based on the GUI of the operating system. If you do not want the form to submit; you can hook to the form's submit event and return false. In fact, hooking to the form's submit function might be the best option for you. Hope that helps!


Alert boxes are now tab-modal in FF, meaning that they block access (and freeze execution) on the current page only and not the whole browser. Apparently, that doesn't stop a different page being loaded.

Even though it's a major change in behaviour, it arguably makes sense.

Depending on your context, you could use the onbeforeunload event to show a message to the user. However, that dialog box will always have an "OK" and a "Cancel" button.

0

精彩评论

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