2013-05-04 120 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中,和一噸其他多功能的方式,適合您的需求。