复选框事件

阿敏·埃夫特克哈里(Amin Eftekhari)

我想创建一个复选框输入,当我们选择它时,它将获得第一个输入的数量并将其减少10个,而当我们取消选择它时,第一个输入打印的原始数量将被减少。例如

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="number" id="input">
    <input type="checkbox" onclick="one()">
    <p id="par">hrllo</p>
    <script>
        function one () {
            let input = document.getElementById("input").value;
            y = input - 10;
            document.getElementById("par").innerHTML = y;
            
            if(!event.target.checked){
           
            document.getElementById("par").innerHTML = input;
            
            }
        }
    </script>
</body>
</html>

阿米特·库玛(Amit Kumar)

您可以定义checkbox元素的id,并在id的基础上可以检查是否选中了checkbox。

这是您更新的代码。

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script type="text/javascript">

    </script>
</head>
<body>
    <input type="number" id="input">
    <input type="checkbox" onclick="one()" id="checkboxId">
    <p id="par">hrllo</p>
    <script>
        function one () {
            var check = document.getElementById("checkboxId");
        if (check.checked) {
            alert("CheckBox checked.");
             let input = document.getElementById("input").value;
            y = input - 10;
            document.getElementById("par").innerHTML = y;
        } else {
            alert("CheckBox not checked."+document.getElementById("input").value);
             document.getElementById("par").innerHTML=document.getElementById("input").value;
            }
        }
    </script>
</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章