2012-03-05 54 views
0
<input type=checkbox id="chk1" name="chk" onclick="getchecked('chk1')"/> 
<input type=checkbox id="chk2" name="chk" onclick="getchecked('chk2')"/> 

getchecked(pId) 
{ 
    if(pId.checked==true) 
    alert('ok'); 
    else 
    alert('not ok'); 
} 

添加checked屬性在我運行時,如果我選中它並沒有爲複選框添加選中的任何財產複選框,我驗證了在開發者工具欄。無法爲複選框

所以它總是進入其他塊爲什麼?

+0

也許是因爲你想要DOM對象「chk1」,但是你要傳遞一個JavaScript字符串'chk1'而不是? – paulsm4 2012-03-05 07:36:37

+1

你的問題是相當不完整的,你在哪裏試圖調用該函數,在任何其他函數中或在複選框中單擊或更改事件本身? – 2012-03-05 07:38:43

回答

1

試試這樣說:

<input type=checkbox id="chk1" name="chk" onclick="getchecked(this)"/> 
<input type=checkbox id="chk2" name="chk" onclick="getchecked(this)"/> 

function getchecked(checkbox) 
{ 
    if(checkbox.checked) 
    alert('ok'); 
    else 
    alert('not ok'); 
} 

即將複選框對象傳遞給函數,而不是它的ID。否則,您必須在功能內部使用var checkbox = getElementById(pId),然後才能撥打checkbox.checked

此外function關鍵字是缺少你的問題(我想這是一個複製/粘貼錯誤)。