icheck使用jquery动态检查复选框或单选按钮

时间:2017-09-08 14:59:51

标签: javascript jquery radio-button icheck

我可以查看下面点击了哪个按钮:

但我需要做的是通过ajax回调,如果我得到值'Y'或'N'然后设置检查单选按钮。请帮我解决这个问题。

$('input').iCheck({
  checkboxClass: 'icheckbox_minimal',
  radioClass: 'iradio_minimal',
  increaseArea: '20%' // optional
});
$('input[type=radio][name=kitchen]').on('ifChecked', function() {
  if (this.value === "N") {
    alert('No');
  } else {
    alert('Yes');
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.2/icheck.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.2/skins/all.css" rel="stylesheet" />
<div class="checkbox">
  <label>Is Kitchen <input type="radio" name="kitchen" value="Y" class="flat"> 
     Yes <input type="radio" name="kitchen" value="N" class="flat"> No
    </label>
</div>

1 个答案:

答案 0 :(得分:1)

进行ajax调用,然后在回调中检查返回的数据并使用iCheck()函数检查所需的元素。

示例:

$.get('url', {}, function( data ){
    if ( data === "Y" ){
        $(':radio[name=kitchen][value=Y]').iCheck('check');
    } else {
        $(':radio[name=kitchen][value=N]').iCheck('check');
    }
});

希望这有帮助。

&#13;
&#13;
$('input').iCheck({
  checkboxClass: 'icheckbox_minimal',
  radioClass: 'iradio_minimal',
  increaseArea: '20%' // optional
});

var data = "N";

if ( data === "Y" ){
    $(':radio[name=kitchen][value=Y]').iCheck('check');
} else {
    $(':radio[name=kitchen][value=N]').iCheck('check');
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.2/icheck.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/iCheck/1.0.2/skins/all.css" rel="stylesheet" />
<div class="checkbox">
  <label>Is Kitchen <input type="radio" name="kitchen" value="Y" class="flat"> 
     Yes <input type="radio" name="kitchen" value="N" class="flat"> No
    </label>
</div>
&#13;
&#13;
&#13;