js中,单选按钮radio的使用

(1)在html中,代码如下:(解析:一行"row"为col-xs-12)

<label class="bg-info">公开方式</label>
<input id="isOpen" class="form-control formValue" value="" style="display: none;">
<div class="row">
    <div class="col-xs-6">
         <input type="radio" value="open" id="open">公开
     </div>
     <div class="col-xs-6">
         <input type="radio" value="private" id="private" >私密
     </div>
</div>

(2)在js中,实现单选按钮操作:

$("#open").click(function() {
    $("#isOpen").val($("#open").val());
    $("#private").attr("checked", false);
});
$("#private").click(function() {
   $("#isOpen").val($("#private").val());
   $("#open").attr("checked", false);
});

如果对单选按钮初始化复制操作:$("#open").attr("checked", true);

或者:$(":radio[value=" +degree+ "]").attr("checked","checked");

大家好,我是威5,更多精彩关注wx公众号:

posted @ 2017-04-04 22:03  小码农成长记  阅读(23459)  评论(0编辑  收藏  举报