Home > Web Front-end > JS Tutorial > How to solve the problem that the checkbox fails after being selected again?

How to solve the problem that the checkbox fails after being selected again?

零下一度
Release: 2017-07-18 15:36:14
Original
1766 people have browsed it

The traditional selection and cancellation code is as follows:

[html] view plain copy
 print?
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="utf-8">  
    <meta http-equiv="X-UA-Compatible" content="IE=edge">  
    <title></title>  
    <script type="text/javascript" src="../js/jquery-1.11.2.min.js"></script>  
</head>  
<body>  
    <input type="checkbox" name="checkbox" id="checkbox" >  
    <input type="button" onclick="btn_submit();" name="" id="btn_submit" value="选中">  
    <input type="button" onclick="btn_cancel();" id="btn_cancel" value="取消选中">  
</body>  
    <script>  
        var btn_cancel = function(){  
            <span style="color:#ff0000;">$("#checkbox").attr(&#39;checked&#39;,false);</span>  
        };  
        var btn_submit = function(){  
            <span style="color:#ff0000;">$("#checkbox").attr(&#39;checked&#39;,true);</span>  
        };  
    </script>  
</html>
Copy after login

//Problem point

The initial state check box is not all selected,

Click to select all The button calls the checkAll method,

realizes the selection of all,

and then clicks the “Deselect All” button,

realizes the deselection of all,

Then click the Select All button again,

but the result is not all selected,

No response will occur if you click it repeatedly.

demo:

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title><script src="js/jquery-1.11.3.min.js?1.1.11"></script><script>$(function () {
            $("#allchoose").click(function () {
                    $(":checkbox").prop("checked",true);
            })
            $("#allnochoose").click(function () {if ($(":checkbox").is(":checked")){
                    $(":checkbox").attr("checked",false);
                }
            })
            $("#choose").click(function () {if($(":checkbox").is(":checked")){
                    $(":checkbox").attr("checked",false);
                }else{
                    $(":checkbox").prop("checked",true);
                }
            })
        })</script></head><body><input type="checkbox" name="checkbox" id="">乒乓球<input type="checkbox" name="checkbox" id="">羽毛球<input type="checkbox" name="checkbox" id="">足球<input type="checkbox" name="checkbox" id="">篮球<input type="checkbox" name="checkbox" id="">排球<br><input type="button" value="全选" id="allchoose"><input type="button" value="全不选" id="allnochoose"><input type="button" value="反选" id="choose"><input type="submit" value="提交" id="submit"></body></html>
Copy after login

The above is the detailed content of How to solve the problem that the checkbox fails after being selected again?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template