Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 限制引导上切换开关的数量_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

Javascript 限制引导上切换开关的数量

Javascript 限制引导上切换开关的数量,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我目前正在使用is-there脚本,以便限制切换开关的数量。这是我的密码 $(函数(参数){ $('[type=“checkbox”]')。bootstrapSwitch(); 风险价值限额=5; $('[type=“checkbox”]')。打开('switchChange.bootstrapSwitch',函数(事件,状态){ //事件侦听器 }); }); 试试: jsIDLE:试试: jsIDLE:为click事件添加一个额外的事件处理程序,它可以在触发bootstrapSwit

我目前正在使用is-there脚本,以便限制切换开关的数量。这是我的密码


$(函数(参数){
$('[type=“checkbox”]')。bootstrapSwitch();
风险价值限额=5;
$('[type=“checkbox”]')。打开('switchChange.bootstrapSwitch',函数(事件,状态){
//事件侦听器
});
});

试试:

jsIDLE:

试试:


jsIDLE:

为click事件添加一个额外的事件处理程序,它可以在触发bootstrapSwitch之前取消开关更改。像这样:

$('[type="checkbox"]').on('click',function(e) {
       if($(this).siblings(':checked').length >= limit) {
         e.preventDefault();
         alert("You have already reached the limit");
         return false;
       }
});

为click事件添加一个额外的事件处理程序,它可以在触发bootstrapSwitch之前取消switchChange。像这样:

$('[type="checkbox"]').on('click',function(e) {
       if($(this).siblings(':checked').length >= limit) {
         e.preventDefault();
         alert("You have already reached the limit");
         return false;
       }
});

做这样的事-

var limit = 5;
$('[type="checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
    if($('[type="checkbox"]:checked').length > limit ) {
        alert('You are allowed to check '+limit+' options'); /*show alert when more than limit are selected*/
        $(this).prop('checked', false); /* uncheck the last one checked*/
    }
});

做这样的事-

var limit = 5;
$('[type="checkbox"]').on('switchChange.bootstrapSwitch', function(event, state) {
    if($('[type="checkbox"]:checked').length > limit ) {
        alert('You are allowed to check '+limit+' options'); /*show alert when more than limit are selected*/
        $(this).prop('checked', false); /* uncheck the last one checked*/
    }
});

你能进一步解释一下你到底想达到什么目的吗?我有5个开关。我想要的是我只想要(n)个开关被打开。如果启用了(n)个开关,如果用户尝试切换另一个开关,则应显示警报消息,然后将不会切换按下的开关。您能否进一步解释您到底想要实现什么?我有5个切换开关。我想要的是我只想要(n)个开关被打开。如果启用了(n)个开关,如果用户尝试切换另一个开关,它将显示警报消息,然后不会切换按下的开关。这可以正常工作。但我不想切换到推过去。我已经解决了它。谢谢你的帮助。此行解决了问题
$(this).bootstrapSwitch('toggleState')这很好用。但我不想切换到推过去。我已经解决了它。谢谢你的帮助。此行解决了问题
$(this).bootstrapSwitch('toggleState')