按钮有什么问题?

时间:2019-03-08 14:30:55

标签: javascript

我想对按钮单击设置警报,但是当我单击按钮时,没有任何反应,请检查。 即使我单击该按钮,如果该框是选中的然后是foo还是没有选中的也应该是一个警告,然后单击bar却没有任何反应!-开始摘要:js hide:false console:true babel:false- ->

function myFunction(){
if (document.getElementById('foo').checked) {
    alert('foo');
} else {
    alert('bar');
}
}
<input type="radio" name="zing" id="foo" />
<input type="radio" name="zing" id="bar"/>
<button onclick="myFunction()">
hello
</button>

1 个答案:

答案 0 :(得分:2)

常用onclick用小写字母键入

function myFunction(){
if (document.getElementById('foo').checked) {
    alert('foo');
} else {
    alert('bar');
}
}
<input type="radio" name="zing" id="foo" />
<input type="radio" name="zing" id="bar"/>
<button onclick="myFunction()">
hello
</button>