2012-06-27 40 views
2

所以,我有3个单选按钮:jQuery是单选按钮选中

<input type="radio" name="status" value="enabled" class="radio" <?php if($news_row['status']==1) echo 'checked'; ?> /> 
<input type="radio" name="status" value="disabled" class="radio" <?php if($news_row['status']==2) echo 'checked'; ?> /> 
<input type="radio" name="status" value="timer" class="radio" <?php if($news_row['status']==3) echo 'checked'; ?> /> 

而且我想,以检查是否有值定时器单选按钮被选中,像这样

if($('.radio [value=timer]').is(':checked')) { 
    console.log('timer'); 
} 

但很明显由于上面的代码不起作用,我做错了什么。那么,这是正确的做法吗?使用.change()

回答

4

演示http://jsfiddle.net/m4m57/5/

问题是space这里f ($('.radio [value=timer]').is(':checked')) {

        ^----------- 

希望这有助于

代码

$('input').on('change', function() { 
    if ($('.radio[value=timer]').is(':checked')) { 
     alert('say what'); 
     console.log('timer'); 
    } 
});​ 
1

删除空间

if($('.radio[value=timer]').is(':checked')) { 
    console.log('timer'); 
} 

您在.radio [value=timer]有空间,这意味着一个不同的选择例如,它意味着select all elements with value attribute set inside an element with class radio at any nested level

卸下空间意味着select all elements with class radio AND value attribute set