2012-06-27 37 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