简体   繁体   English

模糊事件触发javascript确认后提交事件未触发

[英]submit event not firing after blur event triggers javascript confirmation

I have a simple form, where on blur event of textfield I am showing a confirm message to the user, which simply auto populates another hidden textfield. 我有一个简单的表单,当文本字段发生模糊事件时,我向用户显示一条确认消息,该消息将自动填充另一个隐藏的文本字段。 Which works good so far. 到目前为止效果不错。 The problem happens when the user directly clicks the submit button instead of clicking another field or anywhere else on the form. 当用户直接单击“提交”按钮而不是单击另一个字段或表单上的其他任何位置时,就会发生问题。 This should trigger both the blur and submit events, but for some reason the blur triggers, the confirmation comes up, and once the user clicks on the confirmation, submit event isn't triggered. 这应该同时触发模糊和提交事件,但是由于某种原因,模糊触发,确认出现,并且一旦用户单击确认,就不会触发提交事件。 User is forced to click submit again to trigger submit event. 用户被迫再次单击提交以触发提交事件。 Any ideas? 有任何想法吗?

$(function(){
   $("#txt_fld1").blur(function(){
      if(confirm("Auto calculate charge?"))
      {
         $("#txt_chrg").val("8.25");
      }
   });
   $("form").submit(function(){
      alert("SUBMITTING");
   });
})

This might help: 这可能会有所帮助:

$("button[type=submit]").click(function(e) {
e.preventDeafault();
#("#txt_fld1").blur();
$("form").submit();
});

Consider using jQuery UI's custom confirmation dialog. 考虑使用jQuery UI的自定义确认对话框。

You can pass callbacks to Ok and Cancel events so that you can ensure that submit() gets triggered always. 您可以将回调传递给OkCancel事件,以便确保始终触发submit()

In fact, you can trigger other validations before submit programmatically. 实际上,您可以在以编程方式提交之前触发其他验证。

http://jqueryui.com/demos/dialog/#modal-confirmation http://jqueryui.com/demos/dialog/#modal-confirmation

It seems that this is because "submit" requires a click-event on the submit button. 看来这是因为“提交”要求在提交按钮上单击事件。 But click does not actually occurs, cause when you perform a mousedown, then confirm pops up and consequently a blur-event on the submit button, ie mouseup-event does not occur, so there is no click. 但是实际上并没有发生单击,这是在执行鼠标按下时引起的,然后确认弹出,并因此在提交按钮上出现了模糊事件,即没有发生鼠标上升事件,因此没有单击。 So when confirm is closed submit-button has already lost its focus and no submit occurred. 因此,当确认关闭时,提交按钮已经失去了焦点,没有提交发生。

You have to manually submit a form, using a handler on mousedown-event 您必须使用mousedown-event上的处理程序手动提交表单

$("input:submit").mousedown(function(e){
    e.preventDefault();
    //$("#txt_fld1").blur();
    $('form').submit();
});

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

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