Javascript Jquery复选框max checked
由于某些原因,我的代码不能正常工作,当长度>5时,它应该禁用复选框。这是我的密码:Javascript Jquery复选框max checked,javascript,php,jquery,Javascript,Php,Jquery,由于某些原因,我的代码不能正常工作,当长度>5时,它应该禁用复选框。这是我的密码: <script type="text/javascript"> var $checks = $(".bowling").change(function () { if ($checks.filter(":checked").length<=5) { $(".formular").toggle($checks.filter(":che
<script type="text/javascript">
var $checks = $(".bowling").change(function () {
if ($checks.filter(":checked").length<=5)
{
$(".formular").toggle($checks.filter(":checked").length>0);
$("html, body").animate({scrollTop: $(".formular").offset().top});
}
else
{
$checks.filter(":checked").attr("disabled", "disabled");
}
});
</script>
var$checks=$(“.bowling”).change(函数(){
如果($checks.filter(“:checked”).length0);
$(“html,body”).animate({scrollTop:$(“.formular”).offset().top});
}
其他的
{
$checks.filter(“:checked”).attr(“禁用”、“禁用”);
}
});
html它在一段时间内:
<label class="ora_bg" data-toggle="tooltip" data-trigger="click hover" data-placement="bottom" >
<input class="bowling" type="checkbox" value="'.$row['Interval'].'" name="interval[]" class="checkbox" id="i'.$row['Interval'].'">
<h3 class="ora2_bg">'.$row['Interval'].'</h3>
</label>
“.$row['Interval']”
var$checks=$(“.bowling”).change(函数(){
如果($checks.filter(“:checked”).length这对我在JSFIDLE上的工作很好:
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
$(document).ready(function(){ // I have just added this
var $checks = $(".bowling").change(function () {
if ($checks.filter(":checked").length<=5)
{
$checks.not(":checked").removeAttr("disabled");
$("html, body").animate({scrollTop: $(".formular").offset().top});
}
else
{
$checks.not(":checked").attr("disabled", "disabled");
}
});
});
$(document).ready(function(){//我刚刚添加了这个
var$checks=$(“.bowling”).change(函数(){
if($checks.filter(“:checked”).lengthvar$checks=$(“.bowling”).change(函数(){
如果($checks.filter(“:checked”)。长度<5)
{
$(“.formular”).toggle($checks.filter(“:checked”).length>0);
//启用所有输入
$checks.not(“:checked”).removeAttr(“disabled”);
}
其他的
{
$checks.not(“:checked”).attr(“disabled”、“disabled”);
}
});
这对我来说很好
在这里,由于您没有提供标记,我已经为您创建了一个可能满足您的需求的虚拟示例
$(函数(){
$(“输入[type='checkbox']”)。在('click',function()上{
警报($(“输入:复选框:未(:选中)”).length;
如果($(“输入:复选框:未(:选中)”)。长度0)
{
var value=checked.val();
$(“输入[name='banners\u image\u local']”)val(值);
}
}
});
});
是否删除了文档准备处理程序以缩短示例?如果没有,则需要一个.lenght of unchecked复选框为5或选中?使用.prop()设置选中状态,如$checks.filter(:checked”).prop(“disabled”,true);
当选中长度=5时,在复选框更改事件中,然后将禁用属性添加到所有未选中的复选框中。似乎。不是获胜答案:d,thnx是working@user3058067,很高兴帮助你。嗯,但这是一个错误,在我取消选中一个复选框后,我无法再选中另一个:-?我已经为你创建了一个虚拟示例,只需选中down@user3058067,我就可以了已更新我的答案。请检查。
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
<input class="bowling checkbox" type="checkbox" value="" type="checkbox" />
$(document).ready(function(){ // I have just added this
var $checks = $(".bowling").change(function () {
if ($checks.filter(":checked").length<=5)
{
$checks.not(":checked").removeAttr("disabled");
$("html, body").animate({scrollTop: $(".formular").offset().top});
}
else
{
$checks.not(":checked").attr("disabled", "disabled");
}
});
});
var $checks = $(".bowling").change(function () {
if ($checks.filter(":checked").length < 5)
{
$(".formular").toggle($checks.filter(":checked").length>0);
// enable all inputs
$checks.not(":checked").removeAttr("disabled");
}
else
{
$checks.not(":checked").attr("disabled", "disabled");
}
});
$(function(){
$("input[type='checkbox']").on('click', function(){
alert($("input:checkbox:not(:checked)").length);
if($("input:checkbox:not(:checked)").length <=1 ){
$("input:checkbox:not(:checked)").attr('disabled', true);
}else{
$("input[type='checkbox']").attr('disabled', false);
var checked = $("input[type='checkbox']:checked");
if(checked.length >0)
{
var value = checked.val();
$("input[name='banners_image_local']").val(value);
}
}
});
});