jquery - 按组获取单选按钮值

标签 jquery radio-button radiobuttonlist

我的单页上有一堆单选按钮,我想从选中的单选按钮中获取值

这是我的 HTML 代码

<div class="row">
//first radio button group
    <div class="col-md-1 col-xs-1 col-sm-1">&nbsp;</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">&nbsp;</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 无用.. radio 未被过滤..

如何解决这个问题?

最佳答案

无需使用循环来获取选定的单选按钮值。

只需使用:checked

$("input[name=pertanyaan1]:checked").val();

$('button').click(function(){
  console.log($("input[name=pertanyaan1]:checked").val());
});
<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">&nbsp;</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">&nbsp;</div>
</div>

<button>Get Selected Value</button>

关于jquery - 按组获取单选按钮值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39743747/

相关文章:

javascript - 如果文件在 FTP 上不存在,是否可以使用 JavaScript/jQuery 隐藏链接?

javascript - 如何使用 jQuery 中的 inArray 比较两个数组中特定键的值

html - 如何在选择时使 HTML 单选按钮加粗?

javascript - 如何使用 Javascript/Jquery 更改单选按钮的值

c# - 从枚举加载单选按钮列表时,如何在每个单选按钮旁边显示文本?

jquery:如果存在具有专有名称的 child ,则应用 jquery .css 规则

javascript - 在随机位置创建重复元素

javascript - 如何防止输入特定格式的小时和分钟值(hh :mm)

c++ - 如何设置多个单选按钮组以获得正确的 Tab 键顺序和键盘交互 (WIN32)?

radiobuttonlist - umbraco 7 如何在宏局部 View 中访问单选按钮列表选定值