2012-01-11 92 views
3

這是我的複選框,其默認情況下應檢查如何監控與prechecked複選框複選框狀態

<input type="checkbox" name="noILN-GLN" id="noILN-GLN" checked="checked" /> 

我現在想的事件,火災時複選框更改,然後做這個(選中)或(未檢查)。這總是回報「檢查」:

$('#noILN-GLN').bind('change', function() { 
    if ($(this).attr('checked')) { 
     console.log("checked") 
     } else { 
      console.log("not checked"); 
      } 
     }); 

我必須改變,以使控制檯告訴我,如果框被選中或沒有?它需要被默認檢查,所以我設置了checked =「checked」。任何其他方式來預先檢查框,這可能有幫助嗎?

謝謝!

回答

4

試試這個

$('#noILN-GLN').bind('change', function() { 
    if (this.checked) { 
     console.log("checked") 
     } else { 
      console.log("not checked"); 
      } 
     }); 
+0

MH。這很容易......謝謝! – frequent 2012-01-11 16:54:26

+3

@frequent您也可以執行'$(this).prop('checked')'。你不需要這個屬性,你需要這個屬性。 – kapa 2012-01-11 17:14:38

+0

@bazmegakapa - 謝謝。道具也聽起來不錯。 – frequent 2012-01-11 21:44:54