2016-10-15 79 views
-2

我有兩個相同項目的下拉菜單。如果在第一個下拉列表中選擇了一個項目,則它不應出現在下一個下拉列表中。在asp.net中下拉驗證

+0

通過將數據重新分配到在第一個下拉 – Denzil

+0

如果您分享您的代碼,你做了什麼,這將是更好的onChange事件的第二個下拉刪除選定的價值。**它不應該出現在下一個下拉列表**中,這是否意味着如果第一個下拉列表與某個項目一起選擇,那麼該項目不應該出現在第二個下拉列表中? –

+0

這看起來像你已經使用級聯'DropDownList'。沒有這個功能,它將無法工作。分享您的代碼並嘗試刪除級聯。 –

回答

0

Working Demo

$(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(); 
    }); 


});