以下是我的结构。
<span class="mrQuestionTable">
<span id="Cell.0.0">
<input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple" value="__1"/>
<span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile channels)</span>
</span>
<span id="Cell.0.1">
<input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple" value="__2"/>
<span class="mrMultipleText" style="">Cost (managing, implementation related costs)</span>
</span>
</span>
我使用下面的代码来计算长度。一旦我点击复选框,它显示我的长度为0.相反它应该显示1.请让我知道为什么会发生这种情况?
alert($('.mrMultiple:checkbox:checked').length);
答案 0 :(得分:1)
试试这个:
$('.mrMultiple').on('change', function() {
alert($('.mrMultiple:checked').length);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="mrQuestionTable">
<span id="Cell.0.0">
<input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple"
value="__1"/>
<span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile
channels)</span>
</span>
<span id="Cell.0.1">
<input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple"
value="__2"/>
<span class="mrMultipleText" style="">Cost (managing, implementation
related costs)</span>
</span>
</span>
&#13;
答案 1 :(得分:1)
我相信你只想在点击一个复选框后检查复选框的长度。否则,警报功能将在页面加载时运行,并且不会检查任何内容。
$(".mrMultiple").on("click", function()
alert($('.mrMultiple:checked').length);
}
答案 2 :(得分:1)
计算页面中存在的复选框数
var c=$('input[type=checkbox]').length;
计算页面中选中的复选框数量
var cc=$('input[type=checkbox]:checked').length
答案 3 :(得分:1)
您需要将其置于点击功能中,或者无论您点击多少次,它都只会运行一次。
$(document).ready(function() {
$("input").on("click", function() {
console.log($('.mrMultiple:checkbox:checked').length);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="mrQuestionTable">
<span id="Cell.0.0">
<input type="checkbox" name="_QC05_C__1" id="_Q0_C0" class="mrMultiple"
value="__1"/>
<span class="mrMultipleText" style="">Mobile-related fraud (Fraud via mobile
channels)</span>
</span>
<span id="Cell.0.1">
<input type="checkbox" name="_QC05_C__2" id="_Q0_C1" class="mrMultiple"
value="__2"/>
<span class="mrMultipleText" style="">Cost (managing, implementation
related costs)</span>
</span>
</span>