試試這個。
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var mobileNoArray = [];
$(".mob-no").click(function(event){
var selectedNo = $(event.target).val();
var index = $.inArray(selectedNo, mobileNoArray);
if (index != -1) {// means found so pop it
mobileNoArray.splice(index, 1);
} else {
mobileNoArray.push(selectedNo);
}
console.log(mobileNoArray);
alert(mobileNoArray);
});
});
</script>
</head>
<body>
<label>
<input type="checkbox" value="9999999999" class="mob-no"/>
mob-1
</label>
<label>
<input type="checkbox" value="8888888888" class="mob-no"/>
mob-2
</label>
<label>
<input type="checkbox" value="7777777777" class="mob-no"/>
mob-3
</label>
</body>
</html>
在stackoverflow中搜索了很久,終於找到了一個像魅力一樣工作.... – StackOrder 2017-06-08 13:48:38