javascript
  • jquery
  • asp.net
  • drop-down-menu
  • 2011-09-08 61 views 0 likes 
    0

    我有一个下拉列表一个repeater.And这个下拉列表来在repeater.I每个记录显示里面想应该没有重复的值,从下拉列表客户端只能选择。我的代码是使用javascript或jquery检测dropdownlist中选择的重复值?

    <asp:Repeater ID="rep_hello" runat="server"> 
            <ItemTemplate> 
             <asp:Label ID="lbl" runat="server" Text=' <%#DataBinder.Eval(Container.DataItem, "batchid")%>'></asp:Label> 
             <%#DataBinder.Eval(Container.DataItem, "batchid")%><br /> 
             <%#DataBinder.Eval(Container.DataItem, "ts")%><br /> 
             <asp:DropDownList ID="drp_comp" runat="server"> 
              <asp:ListItem Value=""></asp:ListItem> 
              <asp:ListItem Value="1">1</asp:ListItem> 
              <asp:ListItem Value="2">2</asp:ListItem> 
              <asp:ListItem Value="3">3</asp:ListItem> 
             </asp:DropDownList> 
            </ItemTemplate> 
           </asp:Repeater> 
    

    所以请帮助我..在此先感谢。

    +0

    检查了这一点http://stackoverflow.com/questions/878852/jquery- add-decimal-to-select-no-duplicates-with-option-to-remove – birkof

    +0

    先生,这是一些其他情况..我想当任何值在下拉列表中选择,然后它会检查它是否重复,如果是的话显示警告框,这(选定的值)已被选中,请选择另一个。 –

    +0

    你能否粘贴它吐出的最终HTML,因为它在javascript中是你想要的:) –

    回答

    0

    我曾尝试下面的代码和它的工作对我来说

    function EmailPriortyCheker() { 
    
        var Email = $('[id$=drpEmailPriorty]').val(); 
        var Pager = $('[id$=drpPagerPriorty]').val(); 
        var CellPhone = $('[id$=drpCellPhonePriorty]').val(); 
    
        if (Email == "") { } else { 
         if (Email == Pager || Email == CellPhone) { 
          alert('Priorty cannot be same.'); 
          $('[id$=drpEmailPriorty]').val(""); 
         } 
        } 
    } 
    function PagerPriortyCheker() { 
        var Email = $('[id$=drpEmailPriorty]').val(); 
        var Pager = $('[id$=drpPagerPriorty]').val(); 
        var CellPhone = $('[id$=drpCellPhonePriorty]').val(); 
    
        if (Pager == "") { } else { 
         if (Pager == Email || Pager == CellPhone) { 
          alert('Priorty cannot be same.'); 
          $('[id$=drpPagerPriorty]').val(""); 
         } 
        } 
    } 
    
    function CellPhonePriortyCheker() { 
        var Email = $('[id$=drpEmailPriorty]').val(); 
        var Pager = $('[id$=drpPagerPriorty]').val(); 
        var CellPhone = $('[id$=drpCellPhonePriorty]').val(); 
    
        if (CellPhone == "") { } else { 
         if (CellPhone == Email || CellPhone == Pager) { 
          alert('Priorty cannot be same.'); 
          $('[id$=drpCellPhonePriorty]').val(""); 
         } 
        } 
    } 
    

    我的HTML代码是:

    <asp:DropDownList ID="drpEmailPriorty" onchange="EmailPriortyCheker();" TabIndex="5" 
            runat="server"> 
            <asp:ListItem Text="Select Order" Value=""></asp:ListItem> 
            <asp:ListItem Text="First" Value="1"></asp:ListItem> 
            <asp:ListItem Text="Second" Value="2"></asp:ListItem> 
            <asp:ListItem Text="Third" Value="3"></asp:ListItem> 
           </asp:DropDownList> 
    
        <asp:DropDownList ID="drpPagerPriorty" onchange="PagerPriortyCheker();" TabIndex="8" 
            runat="server"> 
            <asp:ListItem Text="Select Order" Value=""></asp:ListItem> 
            <asp:ListItem Text="First" Value="1"></asp:ListItem> 
            <asp:ListItem Text="Second" Value="2"></asp:ListItem> 
            <asp:ListItem Text="Third" Value="3"></asp:ListItem> 
           </asp:DropDownList> 
    
    <asp:DropDownList ID="drpCellPhonePriorty" onchange="CellPhonePriortyCheker();" TabIndex="11" 
            runat="server"> 
            <asp:ListItem Text="Select Order" Value=""></asp:ListItem> 
            <asp:ListItem Text="First" Value="1"></asp:ListItem> 
            <asp:ListItem Text="Second" Value="2"></asp:ListItem> 
            <asp:ListItem Text="Third" Value="3"></asp:ListItem> 
           </asp:DropDownList> 
    
    0

    这里有一段jQuery代码做到这一点:

    var $mySelects = $('select[name$=drp_comp]'); 
    var previousValue; 
    
    $mySelects 
    .focus(function(event) { 
        // to save the previous value 
        previousValue = $(this).val(); 
    } 
    .change(function(event) { 
        var newValue = $(this).val(); 
        $mySelects.not($this).each(function() { 
         var $this = $(this); 
         if ($(this).val() === newValue) { 
          alert('The value "' + newValue + '" has already been selected. Choose another one.'); 
          $this.val(previousValue); 
          return false; // stop looping 
         } 
        }); 
    }); 
    

    在可用性方面,我建议你可以禁用其他选择那些已经选择的值。

    如果您对每次更改进行回发,您可以通过设置ListItem.Enabled = false来在代码隐藏中执行此操作。

    如果你想这样做的客户端,这将给想法:

    var $mySelects = $('select[name$=drp_comp]'); 
    
    $mySelects.change(function(event) { 
        var $this = $(this), val = $this.val(); 
        $mySelects.not($this).find('option[value='+val+']').prop('disabled',true); 
    }); 
    

    d。

    相关问题