嘗試了幾件事情,並沒有成功。我在這裏錯過了什麼?我只是想做一個簡單的警報或日誌,我不能。這裏是我有:onclick函數,如果複選框被選中事件
function toggleEmail() {
var isChecked = document.getElementById("subscribe");
if (isChecked.checked) {
console.log('Yep, my function is being called');
}
}
toggleEmail();
<title>JavaScript Challenges</title>
</head>
<body>
<form action="">
<fieldset>
<legend>Email subscriptions</legend>
<p id="subscribe">
<label>
<input type="checkbox" name="subscribe" id="subscribe" onclick="toggleEmail()" />
Yes! I would like to receive all of your free candy!
</label>
</p>
<p id="emailpara">
<label>
你有一個範圍問題,你正確初始化[小提琴](http://jsfiddle.net/hrt3ozma/)工作正常。 (不是「onLoad」,而是「無換行'」) – Teemu