2012-04-27 26 views
1

好了,非常簡單的JQuery的問題,我在努力尋找一個答案:從代碼中的其他地方調用JQuery事件...?

我有一個被稱爲按鈕點擊一個jQuery事件:

$(document).ready(function(){ 
     resetForms('reservation'); 
     $('#form-reservation').submit(function(event){ 
      event.preventDefault(); //the page will no longer refresh on form submit. 
      var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check. 
      document.cancel_res.cancel_agree.checked = false; 
      //document.cancel_res.cancel_agree.disabled = false; 
      document.cancel_res.cancel_button.disabled=true; 
      document.form_reservation.search_button.value="Searching..."; 
      document.form_reservation.search_button.disabled=true; 
      $.ajax({ 
       url: 'inc/searchres.php', 
       type: 'POST', 
       data: 'resid='+resCheck, 
       success: function(data){ //data is all the info being returned from the php file 
        resetForms('reservation'); //clear forms 
        document.form_reservation.search_button.value="Search Reservation"; 
        document.form_reservation.search_button.disabled=false; 
        $('#reservation-id').val(resCheck); //add read ID back into text box 
        var jsonData = $.parseJSON(data); 
        //BLAH BLAH BLAH BLAH 
       } 
      }); 
     }); 
    }); 

功能完美地工作......但是,無論如何不使用提交事件來調用這個函數?我嘗試在$('#form-reservation').submit(function(event){調用之後取出所有內容,並將它放在一個單獨的函數中,然後從提交事件中調用該函數。但是,無論出於何種原因,這都失敗了。

基本上,我希望submit事件仍然會觸發這個函數,但我也希望能夠在其他情況下調用整個函數。提前致謝!

+0

如果您在Firefox中開發這個功能,我強烈建議您安裝Firebug(http://getfirebug.com/),它會讓您更好地瞭解JavaScript失敗的原因。 – 2012-04-27 21:32:17

回答

1

我只會觸發處理程序。

$("#form-reservation").triggerHandler("submit"); 

http://api.jquery.com/triggerHandler/

按照API文檔,這不會導致表單提交,它只是運行的綁定到該事件的處理程序。

+0

這很好,很容易,並且不需要我的代碼的任何更改...謝謝! – tmparisi 2012-04-27 23:18:42

2

這其實很簡單:

var MyFunc = function(event){ 
      typeof event !== 'undefined' ? event.preventDefault() : false; //the page will no longer refresh on form submit. 
      var resCheck = $(this).find('input[class="reservationid"]').val(); //now we have the reservation ID, let's perform our check. 
      document.cancel_res.cancel_agree.checked = false; 
      //document.cancel_res.cancel_agree.disabled = false; 
      document.cancel_res.cancel_button.disabled=true; 
      document.form_reservation.search_button.value="Searching..."; 
      document.form_reservation.search_button.disabled=true; 
      $.ajax({ 
       url: 'inc/searchres.php', 
       type: 'POST', 
       data: 'resid='+resCheck, 
       success: function(data){ //data is all the info being returned from the php file 
        resetForms('reservation'); //clear forms 
        document.form_reservation.search_button.value="Search Reservation"; 
        document.form_reservation.search_button.disabled=false; 
        $('#reservation-id').val(resCheck); //add read ID back into text box 
        var jsonData = $.parseJSON(data); 
        //BLAH BLAH BLAH BLAH 
       } 
      } 

$(document).ready(function(){ 
     resetForms('reservation'); 
     $('#form-reservation').submit(MyFunc); //this calls on submit 
    }); 

//this calls without need of a submit 
MyFunc(); 
+0

在這裏稍作修改 - 只有在事件未定義的情況下才運行'preventDefault'。這應該處理在提交範圍之外調用MyFunc的任何錯誤。 – philwinkle 2012-04-27 21:32:31

+1

'MyFunc(event)'應該是'MyFunc' – 2012-04-27 21:36:43

+0

哪裏,在提交?你必須將事件提供給函數範圍 - 所以你需要傳遞事件。如果您在提交(MyFunc(event))時未提供參數,則事件將爲typeof ='undefined'。 – philwinkle 2012-04-27 21:38:57

相關問題