将事件切换到受限标签的功能 [英] toggle function to limited tags on click event

查看:111
本文介绍了将事件切换到受限标签的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有10个方框,我添加了切换功能,以通过添加活动"类来选择方框.我只想让用户选择6个框.当用户选择6个框时,它不允许选择其他效果良好的框.我的问题是我想再次更改选择,但这不允许我这样做.以下是代码:`

I have 10 boxes, I have added toggle functions to it to select the box by adding class 'active'. I want to let user to select only 6 boxes. When user selects 6 boxes it not letting to select others which is working fine. My problem is I want to change my selection again but it is not letting me to do that. Below is code:`

$('a').click(function() {
     if ($('.active').length < 6) {
         $(this).toggleClass('active')
     } else {
         alert('more then 6 selection not allowed')

     }
 })

`我想更改选择,但不允许这样做,因为如果条件为真.如何取消选择所选框,请帮忙!

`I want to change my selection but it is not letting to do because if condition is true. How to un-select the selected boxes, please help!

推荐答案

您要使用hasClass来确定选择是否可以撤消.

You want to use the hasClass to determine if the selection can be undone.

这样,如果取消选择,则允许.

That way, if its an unselection, it is allowed.

$('a').click(function() {
     if ($('.active').length < 6 || $(this).hasClass('active')) {
         $(this).toggleClass('active')
     } else {
         alert('more then 6 selection not allowed')

     }
 })

这篇关于将事件切换到受限标签的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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