应用场景——双盒选择器
两个select可能会出现重复的情况
排除重复代码如下:
/**
* 删除$fromGroup中与$toGroup重复的option
* @param $fromGroup = $('#' + fromGroup + ' option:selected')
* @param $toGroup = $('#' + toGroup + ' option')
*/
function filterRepeat($fromGroup, $toGroup) {
//方法一:
/*var repeatItems = $.grep($fromGroup, function(v){
// if the item does not exist return true which includes it in the new array
return $toGroup.filter("option[value='" + $(v).val() + "']").length != 0;
});*/
//方法二:
var repeatItems = $fromGroup.filter(function(index){
return $toGroup.filter("option[value='" + $(this).val() + "']").length != 0;
});
$(repeatItems).remove();
}