2017-02-20 10 views
0

私はなぜこれが機能していないのか理解しようとしています、私はボタンをクリックして送信したくありませんが、私は1つ持っている場合は動作します代わりにonchange="this.form.submit()"とその投稿私はAjaxの部分をコード化していない、私はそれを見つけて私の状況のた​​めに働かせましたが、私が知っている限り、$('#ajaxform').submit(function()、提出は提出されていますか? onchange="this.form.submit()"<input type="submit" />は同じタイプの送信ではないのはなぜですか?私は何が欠けていますか?送信AJAX投稿を

<form method="post" action="~/getAJAX.cshtml" id="ajaxform" name="form"> 
     @* -------- Div to hold form elements -------- *@ 
     <div class="reportDateDiv"> 

      @* -------- Text --------- *@ 
      <a class="blackColor fSize18 RPtxt">Reporting Period</a> 

      @* -------- Start day box -------- *@ 
      <input type="text" name="inputDate" spellcheck="false" class="datepickerS metricDateTextbox capitalFirst" 
        onchange="this.form.submit()" value="@inputDate" autocomplete="off" placeholder="@placeholderStartDate.ToString("MMM d, yyyy")" readonly="readonly" /> 

      @* -------- Text --------- *@ 
      <a class="blackColor fSize16 RPtxt RPtxtTo">to</a> 

      @* -------- End day box --------- *@ 
      <input type="text" name="endDate" spellcheck="false" class="datepickerE metricDateTextbox capitalFirst" 
        onchange="this.form.submit()" value="@endDate" autocomplete="off" placeholder="@noEndDate.ToString("MMM d, yyyy")" readonly="readonly" /> 

     </div> 
    </form> 

    <script type="text/javascript"> 
     $('#ajaxform').submit(function() { // catch the form's submit event 
      $.ajax({ // create an AJAX call... 
       data: $(this).serialize(), // get the form data 
       type: $(this).attr('method'), // GET or POST 
       url: $(this).attr('action'), // the file to call 
       success: function (response) { // on success.. 
        $('#here').html(response); // update the DIV 
       } 
      }); 
      return false; // cancel original event to prevent form submitting 
     }); 
    </script> 
+0

あなたの説明テキストを編集することはできますか?なんでしょう?代わりに何を得ていますか? –

+0

私の入力テキストボックスから 'onchange =" this.form.submit() "'を削除し、代わりにフォームにサブミットボタンを入れると、すべてが正常に動作し、バックグラウンドで必要なものが得られ、AJAXのように表示されますしかし、onchange submit thingの場合、AJAXの部分はありません。ちょうどAJAXがないかのようにフォームをポストすると、ページがリロードされます。 @ChrisG –

+0

これを確認できます。フォーム上で 'submit()'を呼び出すことは明らかに 'onsubmit'ハンドラをスキップします。解決策は、ajax呼び出しを関数にラップし、その呼び出しを 'onchange'ハンドラに置くことです。 https://developer.mozilla.org/en/docs/Web/API/HTMLFormElement/submit –

答えて

2
フォームで

使用これを:

<input ... onchange="mySubmit(this.form)" ... > 

変更はこれにスクリプト:我々はそれを理解することができるように

function mySubmit(theForm) { 
    $.ajax({ // create an AJAX call... 
     data: $(theForm).serialize(), // get the form data 
     type: $(theForm).attr('method'), // GET or POST 
     url: $(theForm).attr('action'), // the file to call 
     success: function (response) { // on success.. 
      $('#here').html(response); // update the DIV 
     } 
    }); 
} 
+0

絶対に素晴らしい!ご協力いただきありがとうございます! –

+0

あなたは私の一日を作った!ありがとうございました! –

+0

ブリリアント! jQuery、JS、そして変更されたすべてのものを維持するのは難しいので、あなたの時間と解決に感謝します! –

関連する問題