2012-03-06 9 views
0

필드의 배열이 큰 주문 양식을 만들었습니다. 이제 쿠폰 기능을 추가하여 고객에게 할인 코드 등을 제공 할 수있게되었습니다.양식의 Ajax 단일 필드 제출

"쿠폰 적용"버튼을 클릭 한 후 아약스를 사용하여 단일 필드 (쿠폰 코드)를 제출하는 가장 좋은 방법은 무엇입니까? 아마도 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

레일스에서 ​​(post params와 함께) link_to는 없습니까? – Elliot