gpt4 book ai didi

javascript - 我用 JavaScript 制作的自定义单选按钮有什么问题?

转载 作者:行者123 更新时间:2023-11-28 20:01:52 24 4
gpt4 key购买 nike

我正在尝试用 JavaScript 制作一个简单的单选按钮。问题是,每当我检查 BtnIsChkd (ButtonIsChecked) 的 bool 时,当我期望出现警报框时,什么也没有发生!
这是我的代码:-

<!DOCTYPE html>

<html>
<head>
<title>JavaScript Practice</title>
<style type="text/css">
*{
font-family:ebrima;
}
#radio{
height:30px;
width:30px;
border:2px solid cyan;
border-radius:20px;
float:left;
}
#dot{
z-index:2;
height:20px;
width:20px;
background:cyan;
border-radius:20px;
margin-left:-27px;
margin-top:7px;
float:left;
display:none;
}
</style>

</head>

<body">
<div id="radio" onClick="checkTheButton()"></div>
<div id="dot" style="display:none;" onClick="checkTheButton()"></div>
<button type="button" onClick="testing()">Check</button>
<script>

var BtnIsChkd = false;

function checkTheButton() {

var button = document.getElementById('radio');
var dot = document.getElementById('dot');

if(dot.style.display == "none")
{
dot.style.display = "block";
BtnIsChkd = true;
} else{
dot.style.display = "none";
BtnIsChkd = false;
}
}
function testing(){
if(BtnIsChkd == "true"){
alert("Yes");
}
else if(BtnIsChkd == "false") {
alert("No");
}
}
</script>
</body>
</html>

最佳答案

你的 bool 值有引号。在testing()中使用BtnIsChkd == true而不是“true”,对于false也是如此。

        function testing(){
if(BtnIsChkd == true){
alert("Yes");
}
else if(BtnIsChkd == false) {
alert("No");
}
}

关于javascript - 我用 JavaScript 制作的自定义单选按钮有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21534676/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com