如何防止在选定的jQuery多选时一行重复条目
How do I prevent duplicate entries in a row at chosen jQuery multiple select?
我有几行,几乎没有选择框。每行中有几个具有相同内容的选择框。行中的每个项目只能使用一次,因为每列都有一个分类。如果在选择框中使用某个项目,我希望它在行的其他框中显示为灰色。我试过这个:
function setChosen() {
var config = {
'.chosen-select' : {},
'.chosen-select-deselect': {allow_single_deselect:true},
'.chosen-select-no-single' : {disable_search_threshold:10},
'.chosen-select-no-results': {no_results_text:'Oops, nothing found!'},
'.chosen-select-width' : {width:'95%'}
}
for(var selector in config) {
$(selector).chosen(config[selector]);
}
}
setChosen();
$('.chosen-select').on('change', function() {
var selVal = $(this).val();
var rel = $(this).attr('rel');
$('.'+rel).children('option').each(function() {
if($(this).val() == selVal) {
$(this).attr('disabled',true).siblings().removeAttr('disabled').trigger('chosen:updated');
}
});
});
小提琴
我会这样做:
从触发onchange
事件的选择中,我们可以在 DOM 中使用 jQuery 的 parent()
向上移动到它的父级。然后选择该父级中的所有select
元素。循环访问每个选择。首先将选项重置回 enabled
,最后禁用与触发事件的选择的选定值匹配的选项。
$('.chosen-select').on('change', function() {
var self = this; //create a reference to the owner of the event.
var selVal = $(this).val(); //selected value
var parent = $(this).parent(); //select parent row
//traverse all children in that row that are selects
parent.children('select').each(function() {
$(this).find("option").attr("disabled", false); //set all disabled attributes to false. Basically a reset!
if (this != self) //skip the select that is the owner of the event
{
//set the other options to disabled that matches the value of the select.
$(this).find("option[value='"+selVal+"']").attr("disabled", true);
}
});
});
相关文章:
- 防止Iframe窗体在新窗口中打开
- 有没有一种方法可以防止img get请求使用css或js发生
- 防止Alt+Shift默认操作或检测多种操作系统语言的Javascript
- 如何防止网页加载后自动启动功能
- 拆分文本以每隔n个字符添加一行新行,并注意空格
- 动态插入的表:JQuery未检测到最后一行
- 当json解析空响应时,Whatwg-Fetch失败,我该如何防止它
- 防止默认锚点行为AngularJS
- 如何防止jQueryonclick事件中的Ruby方法在页面刷新时执行
- 防止ng重复中的重复值(AngularJS)
- 在终端中运行 JavaScript 时(使用 rhino),如何使用 print() 函数在一行中打印
- 如何在Angular UI网格中选择下一行
- ExtJS网格单元格编辑器,防止焦点松动问题
- 如何防止在选定的jQuery多选时一行重复条目
- 如果最后一行已经显示,如何防止jQuery加载更多停止函数
- 递归可防止函数在一行中被多次单击
- 如何防止谷歌闭包编译器删除一行
- jQuery,$.each如何防止表转到下一行
- 防止在一行代码中出现未定义项
- 当我触发这个jquery事件时,表的另一行中的其他元素也被触发了,这是我想要防止的