簡體   English   中英

單選按鈕對象不支持選中的方法,但我使用的是以前曾使用過的語法

[英]Radio button object doesn't support method checked, but I'm using syntax that has worked before

我桌上有一組單選按鈕。 我正在嘗試默認檢查Crew B收音機。 在我所有的其他標記和代碼中,這是我在表中獲得的內容。 但是,我在javascript運行的行上收到“對象不支持此屬性或方法”的信息。 知道這里發生了什么嗎?

<td width="80px"><input type="radio" name="crew" id="CrewA" value="A">Crew A  </input></td>
<td width="80px"><input type="radio" name="crew" id="CrewB" value="B">Crew B  </input></td>
<td width="80px"><input type="radio" name="crew" id="CrewC" value="C">Crew C  </input></td>
<td width="80px"><input type="radio" name="crew" id="CrewD" value="D">Crew D  </input></td>
<script>document.getElementByID('CrewB').checked = true;</script>

這是我執行此操作的腳本:

“ checked”不是一個真/假值。 這是一個布爾型屬性。

采用:

document.getElementById('CrewB').setAttribute('checked', 'checked')

如果您在控制台中查看,則會看到錯誤消息: Uncaught TypeError: Object #<HTMLDocument> has no method 'getElementByID'

查看代碼,您將看到經典的粘滯鍵。

document.getElementByID('CrewB').checked = true;
                      ^

錯字是錯誤,請修正錯字

document.getElementById('CrewB').checked = true;
                      ^

並且代碼將起作用。 JSFiddle

如果總是要檢查B機組,為什么不只在HTML中添加checked =“ checked”作為屬性,這樣就不需要使用javascript。

好吧,如果默認情況下始終將其checked ,則可以將其放在其標記上,如下所示:

<input type="radio" name="crew" id="CrewB" value="B" checked>Crew B</input>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM