【问题标题】:set asp.net dropdown selected value with jquery使用 jquery 设置 asp.net 下拉列表中的选定值
【发布时间】:2013-07-31 09:01:58
【问题描述】:

我有一个代码应该可以很好地在 asp.net 下拉列表上设置选定的值,但由于某种原因,它不想在我的情况下工作,尽管它在我一直在从事的其他项目中正常工作. 所以这是下拉列表:

  <asp:DropDownList ID="ddlPerc" runat="server">
                             <asp:ListItem Value="">Select Percentage</asp:ListItem>
                             <asp:ListItem Value="0.01">0.01</asp:ListItem>
                             <asp:ListItem Value="0.05">0.05</asp:ListItem>        
                 <asp:ListItem Value="0.1">0.1</asp:ListItem>  
                             <asp:ListItem Value="0.15">0.15</asp:ListItem>
                             <asp:ListItem Value="0.2">0.2</asp:ListItem>    
                             <asp:ListItem Value="0.25">0.25</asp:ListItem>  
                             <asp:ListItem Value="0.3">0.3</asp:ListItem>   
                             <asp:ListItem Value="0.4">0.4</asp:ListItem>                                                                
                             <asp:ListItem Value="0.48">0.48</asp:ListItem>                                                                  
                             <asp:ListItem Value="0.5">0.5</asp:ListItem>
                             <asp:ListItem Value="0.52">0.52</asp:ListItem>      
                             <asp:ListItem Value="0.6">0.6</asp:ListItem>
                             <asp:ListItem Value="0.7">0.7</asp:ListItem>
                             <asp:ListItem Value="0.75">0.75</asp:ListItem>                                                                  
                             <asp:ListItem Value="0.8">0.8</asp:ListItem>                                 
                             <asp:ListItem Value="0.85">0.85</asp:ListItem>    
                             <asp:ListItem Value="0.9">0.9</asp:ListItem>                                 
                             <asp:ListItem Value="0.95">0.95</asp:ListItem>
                             <asp:ListItem Value="0.99">0.99</asp:ListItem>
                             <asp:ListItem Value="1">1</asp:ListItem>
                             </asp:DropDownList>

应该工作的jquery代码是这样的:

  $(document).on("click", ".open-AddBookDialog", function () {                                       
              var sign = "1";
         $('dropdownAgentPerc option')
        .filter(function () { return $.trim($(this).val()) == sign; })
         .attr('selected', true);
              $('#addBookDialog').modal('show');

当我检查页面的来源时,我可以看到该值的标签 Selected 为 true,但在模式弹出窗口中该值仍未被选中。 知道可能是什么原因吗? 在此先感谢 Laziale

【问题讨论】:

    标签: jquery asp.net jquery-ui twitter-bootstrap drop-down-menu


    【解决方案1】:

    试试

    .prop('selected', true);
    

    而不是

    .attr('selected', true);
    

    【讨论】:

    • 假设他使用的是 jQuery 1.6 或更高版本,是的
    • 谢谢,我正在使用 jquery 1.8.3,但仍然不想工作。如果有人有时间,他们可以连接到我的电脑并查看其外观
    【解决方案2】:

    试试这个,

    CssClass 添加到DropDownList(例如ddl),并使用$(".ddl").val("your_value"); 设置值

    【讨论】:

      猜你喜欢
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 1970-01-01
      • 1970-01-01
      • 2010-11-21
      相关资源
      最近更新 更多