在做一個商城的購物車的時候遇到了一個坑, 購物車一般都有全選按鈕, 再次點擊就會全部消除, 在網上查到的答案全部都是使用attr來做的, 無一例外都不能用, 之后才知道要使用jquery的prop
和removeProp
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo</title>
<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
isChecked = false;
$('#all').click(function(){
isChecked = !isChecked;
if(isChecked){
$('.option').prop("checked",true)
}else{
$('.option').removeProp("checked")
}
})
})
</script>
</head>
<body>
<input type="checkbox" value="1" class="option">1</input>
<br>
<input type="checkbox" value="2" class="option">2</input>
<br>
<input type="checkbox" value="3" class="option">3</input>
<br>
<input type="checkbox" value="4" class="option">4</input>
<br>
<input type="checkbox" value="全選" id="all">全選</input>
</body>
</html>