2012-03-06 12 views
0

私はオーダーフォームを作成しましたが、その中には大きなフィールド配列があります。私は現在クーポン機能を追加しており、割引コードなどを提供することができます。フォーム内のAjaxシングルフィールド送信

「クーポンを適用」ボタンをクリックした後、ajaxを使って1つのフィールド(クーポンコード)を送信する最良の方法は何ですか?おそらくUJS(?)を使用してフロントエンドで価格を更新できることは明らかです。最終的な価格計算はバックエンドで行われることは明らかです。

乾杯。

答えて

1

JSフレームワークを使用してAjaxリクエストを行うことをお勧めします。あなたはJQueryの場合、与えられた例を使ってsimple postを行う方法を理解することができます。

<!DOCTYPE html> 
<html> 
<head> 
    <script src="http://code.jquery.com/jquery-latest.js"></script> 
</head> 
<body> 
    <form action="/" id="searchForm"> 
    <input type="text" name="s" placeholder="Search..." /> 
    <input type="submit" value="Search" /> 
    </form> 
    <!-- the result of the search will be rendered inside this div --> 
    <div id="result"></div> 

<script> 
    /* attach a submit handler to the form */ 
    $("#searchForm").submit(function(event) { 

    /* stop form from submitting normally */ 
    event.preventDefault(); 

    /* get some values from elements on the page: */ 
    var $form = $(this), 
     term = $form.find('input[name="s"]').val(), 
     url = $form.attr('action'); 

    /* Send the data using post and put the results in a div */ 
    $.post(url, { s: term }, 
     function(data) { 
      var content = $(data).find('#content'); 
      $("#result").empty().append(content); 
     } 
    ); 
    }); 
</script> 

</body> 
</html> 
+0

Railsにlink_to(ポストパラメータあり)はありませんか? – Elliot

関連する問題