Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 复选框事件_Javascript_Html - Fatal编程技术网

Javascript 复选框事件

Javascript 复选框事件,javascript,html,Javascript,Html,我想做一个复选框输入,当我们选择它时,它会得到第一次输入的数量,并将其减少10,当我们取消选择它时,第一次输入打印的原始数量。 比如说 文件 hrllo 功能一(){ 让输入=document.getElementById(“输入”).value; y=输入-10; document.getElementById(“par”).innerHTML=y; 如果(!event.target.checked){ document.getElementById(“par”).innerHTML=输入

我想做一个复选框输入,当我们选择它时,它会得到第一次输入的数量,并将其减少10,当我们取消选择它时,第一次输入打印的原始数量。 比如说


文件

hrllo

功能一(){ 让输入=document.getElementById(“输入”).value; y=输入-10; document.getElementById(“par”).innerHTML=y; 如果(!event.target.checked){ document.getElementById(“par”).innerHTML=输入; } }
您可以检查事件对象以查找复选框的值。根据复选框值,您可以添加或减去输入元素的值


文件

hrllo

功能一(活动){ 让输入=document.getElementById(“输入”).value; y=event.target.checked?输入-10:输入; document.getElementById(“par”).innerHTML=y; }
您可以检查事件对象以查找复选框的值。根据复选框值,您可以添加或减去输入元素的值


文件

hrllo

功能一(活动){ 让输入=document.getElementById(“输入”).value; y=event.target.checked?输入-10:输入; document.getElementById(“par”).innerHTML=y; }
您可以定义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>


文件

hrllo

功能一(){ var check=document.getElementById(“checkboxId”); 如果(勾选,勾选){ 警告(“选中复选框”); 让输入=document.getElementById(“输入”).value; y=输入-10; document.getElementById(“par”).innerHTML=y; }否则{ 警报(“未选中复选框”。+document.getElementById(“输入”).value); document.getElementById(“par”).innerHTML=document.getElementById(“input”).value; } }
您可以定义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>


文件

hrllo

功能一(){ var check=document.getElementById(“checkboxId”); 如果(勾选,勾选){ 警告(“选中复选框”); 让输入=document.getElementById(“输入”).value; y=输入-10; document.getElementById(“par”).innerHTML=y; }否则{ 警报(“未选中复选框”。+document.getElementById(“输入”).value); document.getElementById(“par”).innerHTML=document.getElementById(“input”).value; } }
实际上,在取消选中时,它没有变回其原始值,下面的解决方案工作正常实际上,在取消选中时它没有变回其原始值,下面的解决方案工作正常