M A*_*ver 1 jquery radiobuttonlist radio-button
我的单页上有一堆单选按钮,我希望从已选中的单项中获取一个值
这是我的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>
Run Code Online (Sandbox Code Playgroud)
我试过用这样的循环
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++;
});
Run Code Online (Sandbox Code Playgroud)
但那段代码if没用..收音机没有过滤..
怎么解决这个?
无需使用循环来获取选定的单选按钮值.
只是用 :checked
$("input[name=pertanyaan1]:checked").val();
Run Code Online (Sandbox Code Playgroud)
$('button').click(function(){
console.log($("input[name=pertanyaan1]:checked").val());
});Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)