2016-05-01 66 views
-1

我有這樣的html頁面。在很多div裏面都有單選按鈕。我如何使用JavaScript或jQuery獲取它們的值?如何使用JavaScript或jQuery獲取單選按鈕值?

<html> 
<head> 
</head> 

<body> 
<div id="container"> 
    <div id="surveyContainer"> 
     <div class="optionsContainer"> 
      <div class="imgAnsContainer"> 
       <div class="asnwers"> 
        <div class="radioContainer"> 
         <div class="radioWrap"> 
          <input name="tantargy1 - tanar1" value="0" type="radio"> 
          <label>0-20%</label> 
         </div> 
         <div class="radioWrap"> 
          <input name="tantargy1 - tanar1" value="1" type="radio"> 
          <label>20-80%</label> 
         </div> 
         <div class="radioWrap"> 
          <input name="tantargy1 - tanar1" value="2" type="radio"> 
          <label>80-100%</label> 
         </div> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 
</div> 
</body> 

</html> 
+3

你再次問? [我怎樣才能獲得這個HTML頁面的單選按鈕值與JavaScript或jQuery?](http://stackoverflow.com/questions/36966914/how-can-i-get-radio-button-value-in-this-html -page-with-javascript-or-jquery) –

+2

不要雙重張貼。如果你不喜歡你的其他問題的答案,向那些花時間回答的用戶解釋他們的答案是不合適的。 – Turnip

回答

0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 


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

$('input[type="radio"]').on("click",function(){ 

    var result = $('input[type="radio"]:checked') 
if(result.length>0) 
{ 
var resultString =""; 
resultString= "No of Radio button Checked :"+ result.length + "</br>"; 
result.each(function(){ 
    resultString = resultString + $(this).val() + "</br>" ; 
}) 

$('#div1').html(resultString) 
} 
}) 
}); 
</script> 

<html> 
<input type="radio" value="radioButn1" /> 
<input type="radio" value="radioButn2" /> 
<input type="radio" value="radioButn3" /> 
<input type="radio" value="radioButn4" /> 
<input type="radio" value="radioButn5" /> 

<div id="div1"> </div> 
</html> 
+0

請不要回答明確重複。 – Turnip

相關問題