2013-05-04 30 views
0

如何禁用使用ajax的表单提交?在表单提交之前,用户必须先点击ajax对话框中的按钮。在表单提交之前加载ajax对话框

//on form submit 
//I'm using input type submit and not button (as much as possible, I don't want to change that) 

$.ajax({ 
      type: "POST", 
      url: "<?=url::base(TRUE)?>prompt", 
      data: $('#formname').serialize(), 
      async: false, 
      success: function(response){ 
       $("#dialogpromt").html(response); 
       $("#dialogpromt").dialog({ 
        autoOpen: true, 
        height: 'auto', 
        width: '100', 
        resizable: 'false', 
        dialogClass: 'dFixed'      
       });   
       //onlcick event would change a hidden field to '1' 
       //works fine 
      }, 

     }); 


     //Is there a way I can ensure that the dialog above is closed first before the below lines will execute? 
     if(document.getElementById('submitInd').value == "0") 
      return false; 
     else 
      return true;    

在此先感谢!

+0

有必要在窗体提交上调用此操作?还有另外一种方法吗?让我知道 – 2013-05-04 04:54:33

+0

是的,表单提交时需要此操作。 – 2013-05-04 05:23:23

回答

0
$("Your Submit button Id").click(function (event) { 

    event.preventDefault(); // this will be used to stop the reloading the page 

    $.ajax({ 
     type: "POST", 
     url: "<?=url::base(TRUE)?>prompt", 
     data: $('#formname').serialize(), 
     async: false, 
     beforeSend: function() { /// use beforeSend to open a dialog box 

      $("#dialogpromt").dialog({ 
       autoOpen: true, 
       height: 'auto', 
       width: '100', 
       resizable: 'false', 
       dialogClass: 'dFixed' 
      }); 


     }, 
     success: function (response) { 
      $("#dialogpromt").html(response); 

      //tried the if else here, no luck. 
      //the dialog will just load but the form will still continue to submit        
     } 

    }); 
}); 
+0

表单仍提交:[ – 2013-05-04 05:22:01

+0

@me_an你的意思是重新加载页面? – underscore 2013-05-04 05:37:51

+0

是的表单仍然提交/重新加载 – 2013-05-04 05:42:19

0

有几种方法可以使按钮不提交。我开始使用表单时学到的方法是使用跨度而不是按钮。

一个更好的,更简单的方法,因为它可以让你保持你的按键结构,是简单地写

<button type='button'>....</button> 

我不知道正是它,但包括type='button,而不是在type='submit'这是窗体中的默认值,取消默认窗体提交操作。

还有其他方法可以改变这种使用javascript,如手动压制默认动作,甚至不使用表单的第一个地方,而是用类'form'包装物品在一个div中,和一吨其他多功能的方式,适合您的需求。

相关问题