我有一个复选框的字段集,我想知道首先检查了哪个复选框,依此类推。我尝试创建一个数组,当用户检查输入时它会推送值,但它不起作用。
<label for="B" class="mainLabel mainTop" id="labelB">B</label>
<input type="checkbox" name="mainForm[]" id="B" value="B" onClick="return KeepCount()">
<label for="E" class="mainLabel mainTop" id="labelE">E</label>
<input type="checkbox" name="mainForm[]" id="E" value="E" onClick="return KeepCount()">
<label for="LM" class="mainLabel mainTop" id="labelLM">LM</label>
<input type="checkbox" name="mainForm[]" value="LM" id="LM" onClick="return KeepCount()">
<label for="UM" class="mainLabel mainTop" id="labelUM">UM</label>
<input type="checkbox" name="mainForm[]" value="UM" id="UM" onClick="return KeepCount()">
<label for="SL" class="mainLabel mainTop" id="labelSL">SL</label>
<input type="checkbox" name="mainForm[]" value="SL" id="SL" onClick="return KeepCount()">
答案 0 :(得分:0)
这是使用Jquery的快速解决方案。
我必须删除keepCount()
因为它未包含在您的代码中
var click = false;
var arr = [];
$('.mainLabel+input').each(function() {
$(this).change(function() {
if (!click) {
console.log("First= " + $(this).val());
click = true;
} else {
console.log("and so on= " + $(this).val());
}
arr.push($(this));
console.log("Array= " + arr);
})
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label for="B" class="mainLabel mainTop" id="labelB">B</label>
<input type="checkbox" name="mainForm[]" id="B" value="B">
<label for="E" class="mainLabel mainTop" id="labelE">E</label>
<input type="checkbox" name="mainForm[]" id="E" value="E">
<label for="LM" class="mainLabel mainTop" id="labelLM">LM</label>
<input type="checkbox" name="mainForm[]" value="LM" id="LM">
<label for="UM" class="mainLabel mainTop" id="labelUM">UM</label>
<input type="checkbox" name="mainForm[]" value="UM" id="UM">
<label for="SL" class="mainLabel mainTop" id="labelSL">SL</label>
<input type="checkbox" name="mainForm[]" value="SL" id="SL">