如果选中至少两个复选框,如何启用提交按钮? [英] How to enable submit button if at least two checkboxes are checked?

查看:73
本文介绍了如果选中至少两个复选框,如何启用提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助我在此处找到的答案,我尝试禁用提交按钮并在点击时发送提醒消息,直到检查不到至少2个复选框。

With the help of answers I found here, I try to disable submit button and send an alert message when clicked on it until there's not at least 2 checkboxes checked.

我做错了什么?

var selected = $('#frmCompare :checkbox:checked').length;

function verifCompare() {
    if (selected >= 2) {
        //good
        $('#frmCompare').submit();
    } else {
        //bad
        alert('Veuillez selectionner au moins 2 produits à comparer...');
        return false
    }
}

$(document).ready(function () {
    $('#btnCompare').attr('disabled', 'disabled');
    $('#frmCompare :checkbox').change(function () {
        //alert(selected);
        if (selected >= 2) {
            $('#btnCompare').attr('enabled');
        }
    });
});

此时,只有提醒信息有效。

At this point, only alert message works.

小提琴

Fiddle

编辑:添加小提琴

推荐答案

没有启用 HTML中的属性。

There is no enabled attribute in HTML.

$('#btnCompare').prop('disabled', selected < 2);

您还需要重新计算所选的值在每个更改时,您不能只使用在页面加载时设置的内容。

You also need to recalculate the value of selected at every change, you can't just go with what it was set to at page load.

这篇关于如果选中至少两个复选框,如何启用提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆