2012-04-27 3 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 
       } 
      }); 
     }); 
    }); 

機能が完璧に動作が...しかし、とにかく、このイベントを利用せずにこの関数を呼び出すことはありますか?私は呼び出しの後にすべてを取り出して別の関数に置き、関数をsubmitイベントから呼び出しようとしました。しかし、何らかの理由でこれは失敗しました。

基本的には、依然としてsubmitイベントがこの関数をトリガするようにしたいが、私はまた、他の状況下で関数全体を呼び出すことができるようにしたい。前もって感謝します!

+0

Firefoxでこれを開発している場合は、Firebug(http://getfirebug.com/)をインストールすることを強く推奨します。なぜJavaScriptが失敗するのかがわかります。 –

答えて

1

私は単純にハンドラをトリガします。 APIドキュメントを1として

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

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

、これはそれだけでそのイベントにバインドされたハンドラの実行、フォームが提出されることはありません。

+0

これは素晴らしく簡単でしたし、私のコードを変更する必要もありませんでした...ありがとう! – tmparisi

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

+1

'MyFunc(イベント)'は 'MyFunc'にする必要があります –

+0

ここで、関数スコープにイベントを提供する必要があります。イベントを渡す必要があります。 submit(MyFunc(event))で引数を指定しないと、eventはtypeof = 'undefined'になります。 – philwinkle

関連する問題