2016-11-29 15 views
0

私は以下のように2つの送信ボタンを持つフォームを持っています。2つの送信ボタンがあるフォーム

<form id="choice" name='form' method="POST" action="/"> 
    <button type="submit" name="vote" id="btnMinus" value="1"></button> 
    <button type="submit" name="vote" id="btnPlus" value="2"></button> 
</form> 

フォームが2回送信されないようにするため、このコードを使用しています。ただし、「不正な要求」エラーが発生します。それは "投票"値を送信しないようだ。何か案は? RoryMcCrossanさんのコメント@続き

$('#choice').submit(function(e) 
{ 
    e.preventDefault(); 
    $('#btnPlus').attr('disabled',true); 
    $('#btnMinus').attr('disabled',true); 
    this.submit(); 
}); 
+3

ボタンは、ボタンが送信をトリガーするときにのみリクエストに含まれます。あなたは、元のボタンで開始されたイベントをキャンセルし、ボタンのコンテキストなしで新しいサブミットを発生させます。 –

+2

おそらくタイプミスですが、 '$( '#btnPlus')'は2回あります。そして、あなたは '.attr()'の代わりに '.prop()'を使うべきです。 – j08691

+0

@RoryMcCrossanボタンを無効にするための提案はありますか? – Arturo

答えて

1

、あなたは隠された入力を使用して、それは、ボタンがクリックされると、値の設定ができます。

$('#choice button').click(function(e) { 
 
    e.preventDefault(); 
 
    $('#vote-el').val($(this).val()); 
 
    
 
    $('#btnMinus').prop('disabled',true); 
 
    $('#btnPlus').prop('disabled',true); 
 
    
 
    $('#choice').submit(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="choice" name='form' method="POST" action="/"> 
 
    <input id="vote-el" type="hidden" name="vote" value="" /> 
 
    <button type="submit" name="vote" id="btnMinus" value="1">1</button> 
 
    <button type="submit" name="vote" id="btnPlus" value="2">2</button> 
 
</form>

それはの価値ので、何も送信しません
関連する問題