3 回答

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
你需要一個(gè)事件監(jiān)聽(tīng)器
document.getElementById("red").addEventListener("change", myAlert);
function myAlert() {
alert('working');
}
<form>
What color do you prefer?<br>
<input type="radio" name="colors" id="red">Red<br>
<input type="radio" name="colors" id="blue">Blue
</form>

TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超3個(gè)贊
您需要事件偵聽(tīng)器 - 不建議使用內(nèi)聯(lián)事件處理程序
您需要決定要測(cè)試什么
這里有兩種跑步方式
它們是收音機(jī),所以我們不需要測(cè)試它們是否被檢查
window.addEventListener("load",function() { // when the page loads
// testing clicking the RED only
document.getElementById("red").addEventListener("click",function() {
console.log("The specific Red event listener was invoked");
});
// delegation
document.querySelector("form").addEventListener("click",function(e) {
const tgt = e.target;
if (tgt.id==="red") console.log("Clicking in the form, we clicked the thing with ID red")
})
});
<form>
What color do you prefer?<br>
<label><input type="radio" name="colors" id="red">Red</label><br>
<label><input type="radio" name="colors" id="blue">Blue</label>
</form>

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試這樣:
<form>
What color do you prefer?<br>
<input type="radio" name="colors" id="red"/>Red<br>
<input type="radio" name="colors" id="blue"/>Blue
</form>
<script>
document.getElementById("red").onchange = function() {
alert('working');
}
</script>
單擊單選按鈕時(shí)需要一個(gè)事件處理程序來(lái)運(yùn)行該特定代碼。
添加回答
舉報(bào)