我有两个相同项目的下拉菜单。如果在第一个下拉列表中选择了一个项目,则它不应出现在下一个下拉列表中。
答案 0 :(得分:0)
$(function () {
var lastRemovedIndex = -1;
var lastRemovedOptionHtml = null;
$('#ddl1').change(function () {
if (lastRemovedIndex == -1)
{
//both must have equal(and non-zero) number of children
if ($(this).children().length < 1 || $(this).children().length != $('#ddl2').children().length)
return;
else
{
//if a has n children then b must have n-1 as 1 child was removed from b
if ($(this).children().length - 1 != $('#ddl2').children().length)
return;
//Little complex. It says add the last removed option at its last position
if (lastRemovedIndex != $('#ddl2').children().length)
$('#ddl2').children().eq(lastRemovedIndex).before($(lastRemovedOptionHtml));
else
$('#ddl2').append(lastRemovedOptionHtml);
}
var objectToBeRemoved = $('#ddl2').children().eq(this.selectedIndex);
lastRemovedIndex = objectToBeRemoved.index();
lastRemovedOptionHtml = objectToBeRemoved[0].outerHTML;
objectToBeRemoved.remove();
});
});