我有一个表单,其中有许多按行排列的复选框元素。我想实现一个功能,允许用户按住 Cntrl,然后选择一个复选框并让每个框都具有:
A) 如果没有选中初始框,则被选中
B) 从最近的复选框中选中
这可能吗?非常感谢。
最佳答案
这可以帮助你。
该代码用于shift+key
// Usage: $form.find('input[type="checkbox"]').shiftSelectable();
// replace input[type="checkbox"] with the selector to match your list of checkboxes
$.fn.shiftSelectable = function() {
var lastChecked,
$boxes = this;
$boxes.click(function(evt) {
if(!lastChecked) {
lastChecked = this;
return;
}
if(evt.shiftKey) {
var start = $boxes.index(this),
end = $boxes.index(lastChecked);
$boxes.slice(Math.min(start, end), Math.max(start, end) + 1)
.attr('checked', lastChecked.checked)
.trigger('change');
}
lastChecked = this;
});
};
积分:jQuery plugin for shift + click to select multiple checkboxes
关于javascript - 通过 Cntrl + 单击选择多个复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37760375/