如何检查在JavaScript或jquery中是否单击任何单选按钮选项?

时间:2017-05-22 10:04:21

标签: javascript jquery

如果选择了任何选项,我会尝试使用if语句...例如:

    Ext.create('Ext.data.Store', {
        storeId: 'simpsonsStore',
        fields: ['name', 'email', 'phone'],
        data: [{
            name: 'Lisa',
            email: 'lisa@simpsons.com',
            phone: '555-111-1224'
        }, {
            name: 'Bart',
            email: 'bart@simpsons.com',
            phone: '555-222-1234'
        }, {
            name: 'Homer',
            email: 'homer@simpsons.com',
            phone: '555-222-1244'
        }, {
            name: 'Marge',
            email: 'marge@simpsons.com',
            phone: '555-222-1254'
        }]
    });

    Ext.create('Ext.grid.Panel', {
        title: 'Simpsons',
        forceFit : true,
        store: Ext.data.StoreManager.lookup('simpsonsStore'),
        columns: [{
            text: 'Name',
            dataIndex: 'name'
        }, {
            text: 'Email',
            dataIndex: 'email',
            flex: 1
        }, {
            text: 'Phone',
            dataIndex: 'phone'
        }],
        height: 500,
        width: 600,
        renderTo: Ext.getBody()
    });

这是html代码:

<script>
    if(any_selected){
      if(option){
        //do somthing
      }
    }
    else{
      //do somthing else
    } </script

2 个答案:

答案 0 :(得分:0)

尝试如下:

if($('input:[name=optradio]:checked').length)
{
//selected
}
else
{
//not selected
}

如果您想检查上面使用的特定单选按钮组,或者尝试$('input:checkbox:checked').length

答案 1 :(得分:0)

计算:checked次数。

&#13;
&#13;
$(document).ready(function () {
  $('input').change(function () {
    if ($('input:checked').length) {
      $('span').css({'background-color': 'green'});
    } else {
      $('span').css({'background-color': 'red'});
    }
  });
});
&#13;
input {
  margin-left: 10px;
  margin-bottom: 10px;
}

span {
  width: 20px;
  height: 20px;
  background-color: red;
  display: inline-block;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<input type="radio" name="optradio" value=""/>
<hr/>
<span></span>
&#13;
&#13;
&#13;

请注意,您的#radio-c已在循环中,并且每页都会包含非唯一ID。