Welcome to my website, have a nice day!
Dream it, Do it, Make it!

jquery操作input radio单选按钮

整理盘点常见的jquery操作input radio单选按钮的操作:

html代码

<div style="float: left; width: 70%; margin-top:20px;" id="subjectResp">
    <label class="radio-inline"><input type="radio" id="bzjt" name="typeTag" value="01" checked>班子集体责任</label>
    <label class="radio-inline"><input type="radio" name="typeTag" value="02">一把手责任</label>
    <label class="radio-inline"><input type="radio" name="typeTag" value="03">分管领导责任</label>
</div>

1.设置单选按钮选中操作

jquery版本大于等于1.6使用如下方法:

$("#bzjt").prop("checked", true);

jquery版本小于1.6使用如下方法:

$("#bzjt").attr('checked', 'checked');

2.获取点击操作事件

$('input:radio[name=typeTag]').click(function() {
    alert(this.value);
});

3.设置change事件

$('input:radio[name=typeTag]').change(function() {
    alert(this.value);
});

4.获取当前被选中的单选按钮

方法1:

var typeTag = '';
$('input:radio[name=typeTag]').each(function () {
        if(this.checked == true){
            typeTag = this.value;
        }
    });

方法2:

var typeTag = $("input[type='radio'][name='typeTag']:checked").val()
  1. https://www.haorooms.com/post/checkandselect
  2. https://stackoverflow.com/questions/13152927/how-to-use-radio-on-change-event
赞(0)
未经允许禁止转载:Ddmit » jquery操作input radio单选按钮

评论 抢沙发

登录

找回密码

注册