我的单页上有一堆单选按钮,我希望从已选中的单词中获取一个值
这是我的HTML代码
<div class="row">
//first radio button group
<div class="col-md-1 col-xs-1 col-sm-1"> </div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="1"><br>1<!--<br>Awful--></div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="2"><br>2</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="3"><br>3</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="4"><br>4</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="5"><br>5<!--<br>Ok--></div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="6"><br>6</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="7"><br>7</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="8"><br>8</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="9"><br>9</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="10"><br>10<!--<br>Incridible--></div>
<div class="col-md-1 col-xs-1 col-sm-1"> </div>
</div>
我尝试过像这样的循环
var pertanyaan1 = 1;
var isipertanyaan1 = 0;
$("input[name=pertanyaan1]").each(function(){
if($("input[name=pertanyaan1][value="+pertanyaan1+"]").prop("checked","checked"))
{
console.log($("input[name=pertanyaan1][value="+pertanyaan1+"]").prop("checked","checked"));
isipertanyaan1 = pertanyaan1;
// break();
}
// console.log(pertanyaan1);
pertanyaan1++;
});
但该代码使if
无用..收音机未过滤..
如何解决这个问题?
答案 0 :(得分:5)
无需使用循环来获取所选单选按钮值。
只需使用:checked
$("input[name=pertanyaan1]:checked").val();
$('button').click(function(){
console.log($("input[name=pertanyaan1]:checked").val());
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
//first radio button group
<div class="col-md-1 col-xs-1 col-sm-1"> </div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="1"><br>1<!--<br>Awful--></div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="2"><br>2</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="3"><br>3</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="4"><br>4</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="5"><br>5<!--<br>Ok--></div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="6"><br>6</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="7"><br>7</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="8"><br>8</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="9"><br>9</div>
<div class="col-md-1 col-xs-1 col-sm-1 centerText" ><input type="radio" name="pertanyaan1" value="10"><br>10<!--<br>Incridible--></div>
<div class="col-md-1 col-xs-1 col-sm-1"> </div>
</div>
<button>Get Selected Value</button>
&#13;
答案 1 :(得分:0)
此代码可以帮助您
{{1}}