jquery
  • ajax
  • 2010-11-18 3 views 1 likes 
    1

    私はこのコードを以下に示します。私はフォームを提出した後、 "/frontend_dev.php/coche1/new"に行きますが、それは決して行きません(対応するメソッドでdie( "enter")を追加しましたが、 "enter"は表示されません)...jQuery.ajax:最初の手順。それはURLに移動しません

    <script type="text/javascript" src='http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.js'></script> 
    
    <script type="text/javascript"> 
    $(document).ready(function() { 
        $('.formulario').submit(function() { 
         form_data = $.param($(this).serializeArray()); 
         $.ajax({ 
          url: "/frontend_dev.php/coche1/new", 
          type: "POST", 
          data: form_data, 
          success: function() { 
           alert("Data Saved: "); 
          } 
         }); 
        }); 
    }); 
    </script> 
    
    
    <form class="formulario" id="1"> 
        <input type="submit" value="Save"> 
        <label for="coche1_marca">Marca</label> 
        <input type="text" id="coche1_marca" value="FJSDF" name="coche1[marca]"> 
        <label for="coche1_modelo">Modelo</label> 
        <input type="text" id="coche1_modelo" value="FJSDÑF" name="coche1[modelo]"> 
    </form> 
    

    助けが必要ですか?

    よろしく

    ハビ

    +0

    あなたはFirebugコンソールでそれを確認して、AJAXコールが終了していて、コールのステータスはどうですか? –

    答えて

    3

    あなた.submit()ハンドラの最後に偽のリターンを持っている必要があります。それ以外の場合は、AJAXを起動しますが、すぐにフォームを送信すると、フォーム自体にアクションがないため、ページがリロードされます。 return falseを追加すると、AJAXが期待どおりに起動することが示されます。

    +0

    ありがとう、どのように私は "入力"文字列を参照するには、Firebugのコンソールに行っていた。 – ziiweb

    2

    フォームを送信するのはprevent the default actionに何もしないので、action属性で指定されたURLに送信します(この必須属性を指定できなかったため、代わりに現在のURLが使用されます)。これは、XHR要求が返される前に行われるため、成功メソッドは決して起動しません。

    1

    使用ここでは、この

    ... 
    $('.formulario').submit(function (event) 
    { 
        event.preventDefault(); // This will stop the form from submitting. 
        ... 
        return false; // Should also stop the form from submitting, but have ran into cases where it does not. Still, include it for good measure. 
    } 
    

    がイベント引数について心に留めておくべきいくつかの追加のものです:

    event.preventDefault() // Stops default actions, link redirection, form submission, etc... 
    event.stopPropagation() // Stops the event from propagating up the DOM tree. 
    event.stopImmediatePropagation() // Stops the current event and ALL other events on the element from propagating any further up the DOM tree. 
    

    詳細についてはJQueryでのイベントの伝播には、ここを参照してください:http://www.c-sharpcorner.com/UploadFile/satisharveti/665/Default.aspx

    関連する問題