2016-02-09 46 views
1

我有一種形式,就像下面的一個類似:jQuery的 - 遍歷所有選中的單選按鈕

<form id="myForm"> 
     Question 1:<br> 
     <input type="radio" name="question1" value="Yes"> Yes 
     <input type="radio" name="question1" value="No"> No 
     <br> 
     Question 2:<br> 
     <input type="radio" name="question2" value="Yes"> Yes 
     <input type="radio" name="question2" value="No"> No 
     <br> 
     Question 3:<br> 
     <input type="radio" name="question3" value="Yes"> Yes 
     <input type="radio" name="question3" value="No"> No 
     <br> 
     <input type="submit" value="Submit" name="submit"> 
</form> 

我想從使用jQuery的所有問題的所有選擇的單選按鈕值,如果所有值等於「是」,它會提醒成功,否則它會提醒失敗。

這是我寫的jQuery的:

$(document).ready(function(){ 
    $("#myForm input[type=radio]:checked").each(function() { 
     var value = $(this).val(); 
    }); 
}); 
+0

那麼是什麼問題? –

+0

這是演示伴侶嗎?問題是什麼? – guradio

+1

首先你沒有複選框的單選按鈕 – Radu

回答

2

您可以檢查,如果你曾經得到任何無線電選中,那麼結果是失敗,否則成功。

Live Demo

result = "success"; 
$("#myForm input[type=radio]:checked").each(function() { 
    if(this.value == "No" && this.checked == true) 
    { 
    result = "fail"; 
    return false; 
    } 
}); 
alert(result); 
+0

謝謝隊友。它的工作原理:D –

+0

不客氣。 – Adil

0
$(document).ready(function(){ 
    var val=1; 
     $("#myForm input[type=radio]:checked").each(function() { 
      if($(this).val()=="No") 
      { 
       val=2; 
      } 
     }); 

     if(val==1) 
     { 
     alert("Success !!"); 
     } 
     else{ 
     alert("Fail "); 
     } 
    }); 
0
$(document).ready(function(){ 
    var no_found=false; 
    $("#myForm").submit(function(){ 
     $(this).find("input[type=radio]:checked").each(function() { 
     var value = $(this).val(); 
     if(value == "No") 
      no_found=true; 
     }); 
     if(no_found==false){ 
     alert("Success"); 
     } 
     else{ 
     alert("Fail"); 
     } 
    }); 
}); 
0

使用此代碼:

$(function() { 
 
    $('#myForm').on('submit', function(e) { 
 
    e.preventDefault(); 
 

 
    var total = 0; 
 
    $('#myForm input[type="radio"]:checked').each(function() { 
 
     if ($(this).val() == 'Yes') 
 
     total++; 
 
    }); 
 
    if (total == 3) 
 
     alert("Success"); 
 
    else 
 
     alert("Fail"); 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form id="myForm"> 
 
    Question 1: 
 
    <br> 
 
    <input type="radio" name="question1" value="Yes" checked>Yes 
 
    <input type="radio" name="question1" value="No">No 
 
    <br>Question 2: 
 
    <br> 
 
    <input type="radio" name="question2" value="Yes">Yes 
 
    <input type="radio" name="question2" value="No" checked>No 
 
    <br>Question 3: 
 
    <br> 
 
    <input type="radio" name="question3" value="Yes">Yes 
 
    <input type="radio" name="question3" value="No" checked>No 
 
    <br> 
 
    <input type="submit" value="Submit" name="submit"> 
 
</form>

0

試試這個代碼

$("#myForm").submit(function(){ 
    var check = "Yes"; 
    $(this).find("input[type=radio]:checked").each(function() { 
    var value = $(this).val(); 
    if(value == "No") 
     check = "No"; 
    }); 
    alert(check) 
    }); 
0

你可以試試這個問題,以及:

http://codepen.io/anon/pen/JGeLMx

$('#myForm input[type="submit"]').on('click', function(e) { 

    e.preventDefault(); 

    var result = true; 
    $('input[type="radio"]').each(function() { 

    var radio = $(this)[0];  
    if (radio.value === 'Yes' && radio.checked === false) 
     result = false; 

    }); 

    if (result) { 
    alert('Success!'); 
    } else { 
    alert('Fail!'); 
    } 

}); 

迭代所有的單選按鈕,如果在「否」單個檢查或者根本沒有選擇它會失敗,否則,這意味着所有'是'被選中 - 成功。

+0

非常感謝伴侶:) –

+0

沒問題,伴侶。 –

0
<!DOCTYPE html> 
<html> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 


<script> 
    $(document).ready(function() { 

     $('input[type="radio"]').change(function() { 
      var iCountTotal; 
      var iCountCkedYes; 
      iCountTotal = $('input[type="radio"]').length/2; 
      iCountCkedYes = $('input[type="radio"][value="Yes"]:checked').length; 
      if (iCountTotal != iCountCkedYes) { 
       alert('fail') 
      } 
      else { 
       alert('success') 

      } 
     }); 


    }); 


</script> 



<body> 

    <form id="myForm"> 
     Question 1:<br> 
     <input type="radio" name="question1" value="Yes" checked="checked"> Yes 
     <input type="radio" name="question1" value="No"> No 
     <br> 
     Question 2:<br> 
     <input type="radio" name="question2" value="Yes"> Yes 
     <input type="radio" name="question2" value="No" checked="checked"> No 
     <br> 
     Question 3:<br> 
     <input type="radio" name="question3" value="Yes"> Yes 
     <input type="radio" name="question3" value="No" checked="checked"> No 
     <br> 
     <input type="submit" value="Submit" name="submit"> 
</form> 
</html>