【发布时间】:2013-12-18 16:32:44
【问题描述】:
在搜索表单中,我允许用户使用复选框来选择和取消选择许多位置。为了改进搜索查询,一些位置被子位置划分。
在这个JSFIDDLE LINK EXAMPLE 上,英国位置由不同的子位置组成。
过程:
当用户单击英国位置(cf 链接)时,已开发 Jquery 事件来隐藏/显示其子位置。
注意:已经开发了另外两个事件来同步(选中/取消选中)主位置及其子位置。
问题:
当我们多次快速单击“Uk”位置时,Uk 复选框与其子位置 div 容器之间的同步无法正常工作。 例如:可以在不显示子位置的情况下检查英国位置。
有没有办法禁止用户在动画期间点击复选框?
HTML
</ul><ul id="locations"><li id="title"><b>Locations</b></li><div class="list-container">
<li>
<label class="location-checkbox ">
<input value="15" name="locations[]" type="checkbox" > SE Asia - 15 <span></span>
</label>
</li>
<li>
<label class="location-checkbox ">
<input value="8" name="locations[]" type="checkbox" > South America - 8 <span></span>
</label>
</li>
<li class="london-parent-country">
<label class="location-checkbox">
<input id="checkboxuk" value="9" name="locations[]" type="checkbox"> UK - 9 <span></span>
</label>
</li>
<div id="uk-child">
<li>
<label class="location-checkbox child-location">
<input class="uk" value="17" name="locations[]" type="checkbox" style="margin-left:50px;"> England - 17 <span></span>
</label>
</li>
<li>
<label class="location-checkbox child-location">
<input class="uk" value="20" name="locations[]" type="checkbox" style="margin-left:50px;"> Northern Ireland - 20 <span></span>
</label>
</li>
<li>
<label class="location-checkbox child-location">
<input class="uk" value="18" name="locations[]" type="checkbox" style="margin-left:50px;"> Scotland - 18 <span></span>
</label>
</li>
<li>
<label class="location-checkbox child-location">
<input class="uk" value="19" name="locations[]" type="checkbox" style="margin-left:50px;"> Wales - 19 <span></span>
</label>
</li>
</div>
<li>
<label class="location-checkbox ">
<input value="10" name="locations[]" type="checkbox" > Western Europe - 10 <span></span>
</label>
</li>
<br/>
</div></ul><!-- list-container -->
JQUERY
$('li.london-parent-country label').live('click', function (event) {
var ttt= $('#checkboxuk').prop( "checked" )
console.log("every-click: "+ttt);
if($('.london-parent-country').hasClass("animated")){ /* Wait the animation to be completed */ }
else {
// Disable checkboxes during animation - not working
// $('input[value=9]').prop("disabled", true);
// <input type="checkbox" disabled="disabled" checked="checked">
// Add or remove the open class to know if we need to hide/show the div
$('#uk-child').toggleClass("open");
var current_class = $('#uk-child').attr('class');
if (current_class=="open"){
// We show sub-locations
// Add this class to cancelled this function onclick
$('.london-parent-country').addClass("animated");
$("#uk-child").stop().show(1000, "easeOutQuart", function() {
// Remove class when the animation is done.
$('li.london-parent-country').removeClass("animated");
$('input[value=9]').prop("disabled", false);
});
}
else {
// We hide sub-locations
$('.london-parent-country').addClass("animated");
$("#uk-child").stop().hide(1000, "easeOutQuart", function() {
// Remove the animated class + enable checkboxes
$('li.london-parent-country').removeClass("animated");
$('input[value=9]').prop("disabled", false);
});
}
}
});
【问题讨论】:
-
当然有。查看JSFiddle
-
有没有办法让它与自定义复选框图像一起使用?目前正在做一些测试
-
我不推荐它,但您必须为复选框的每个状态制作图像并相应地显示它们,例如 ckbox_unchecked_enabled.gif、ckbox_unchecked_disabled.gif、ckbox_checked_enabled.gif、ckbox_checked_disabled.gif。您必须构建自定义
.on('click', function(){});规则以确定是否应允许用户单击它们。我保证您以后会遇到可维护性问题。