忽略值单选按钮 - iCheck

Ignore value radiobuttons - iCheck

我尝试通过这种方式从单选按钮 (iCheck) 获取值,但始终只 return 第一个单选按钮的值,忽略其余的。理论上说代码很好,但它 return 很糟糕。

Radio box, get the checked value [iCheck]

我的代码:

<div class="step row">
  <div class="col-md-10">
    <h3>YYYYYY.</h3>
    <ul class="data-list-2">
      <li><input name="p1" id="p1" type="radio" class="required check_radio" value="1"><label>Yes</label></li>
      <li><input name="p1" id="p1" type="radio" class="required check_radio" value="0"><label>No</label></li>
      <li><input name="p1" id="p1" type="radio" class="required check_radio" value="2"><label>Meybe</label></li>
    </ul>
  </div>
</div>
<!-- end step -->

我的取值方法:

$(document).ready(function() {
  $('#p1').on('ifChecked', function(event) { //try ifClicked - ifToggled 
    alert($(this).val()); // alert value
  });
});

所有时间 return 值 1 即使选择了另一个选项。

请帮忙,我尝试了整晚不同的方法,但没有用;(

我不确定您是如何为单选按钮生成 ID 的,但是 id 属性 应该始终是唯一的,因为您希望对单选按钮进行相同的事件处理,只需使用 class 以便它将事件附加到属于该 class 的所有单选按钮,那么您不必为每个人添加事件 id:

所以不是这个:

//If you did it this way, you'd have to have one of these blocks for each
//unique id belonging to a radio button
$('#p1').on('ifChecked', function(event){ //try ifClicked - ifToggled 
  alert($(this).val()); // alert value
});

改成这样:

//Use the class instead so that the same event handling can be added to all radio
//buttons in one block of code
$('.required.check_radio').on('ifChecked', function(event){ //try ifClicked - ifToggled 
  alert($(this).val()); // alert value
});