繁体   English   中英

使用jquery设置asp下拉列表的选定值

[英]set selected value of a asp dropdown with jquery

我正在尝试在显示模式弹出窗口时使用jquery设置下拉值。 我正在尝试一些静态数据,但没有任何成功。 这就是我所拥有的:

$(document).on("click", ".open-EditSplitAgent", function () {
              var first = $(this).data('first');
              var last = $(this).data('last');
              var splitPC = $(this).data('splitpc');
              var id = $(this).data('id');
              $('#<%=txtFirstEdit.ClientID%>').val(first);
              $('#<%=txtLastEdit.ClientID%>').val(last);
              $('#<%=hfLifeID.ClientID%>').val(id);              
              $("#ddlAgentPercEdit").val('0.50');             
              $('#editSplitAgent').modal('show');
          });

下拉列表具有以下值:

  <asp:DropDownList ID="ddlAgentPercEdit" runat="server">
                                   <asp:ListItem Value="">Select Percentage</asp:ListItem>
                                 <asp:ListItem Value="0.50">0.5</asp:ListItem>
                                 <asp:ListItem Value="0.75">0.75</asp:ListItem>
                                 <asp:ListItem Value="0.9">0.01</asp:ListItem>
                                 <asp:ListItem Value="0.7">0.7</asp:ListItem>
                                 <asp:ListItem Value="0.1">0.1</asp:ListItem>
                                 <asp:ListItem Value="0.8">0.8</asp:ListItem>
                                 <asp:ListItem Value="0.6">0.6</asp:ListItem>
                                 <asp:ListItem Value="0.9">0.9</asp:ListItem>
                                 <asp:ListItem Value="0.05">0.05</asp:ListItem>
                                 <asp:ListItem Value="0.95">0.95</asp:ListItem>
                             </asp:DropDownList>

我有文本框和隐藏字段,当显示模式弹出窗口时,它具有正确的值。 正如您在jquery事件中看到的那样,我正在尝试静态数据,因此我可以看到它的静态数据正常工作,但最初我需要将splitPC var值分配给下拉值。 提前感谢您的建议,Laziale

编辑:为模态弹出窗口呈现的代码:

        <div class="modal hide fade" tabindex="-1" role="dialog" id="editSplitAgent">
 <div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Edit Split Agent</h3>   
  </div>
    <div class="modal-body">       
        <div class="content">

              <div class="form-row row-fluid">
                      <div class="span12">
                          <div class="row-fluid">
                               <label class="form-label span4" for="tooltip">First Name</label>
                               <input name="ctl00$ContentPlaceHolder1$txtFirstEdit" type="text" id="ctl00_ContentPlaceHolder1_txtFirstEdit" class="span8 tip" />       
                                 <span id="ctl00_ContentPlaceHolder1_RequiredFieldValidator4" style="color:Red;display:none;"></span>
            <input type="hidden" name="ctl00$ContentPlaceHolder1$ValidatorCalloutExtender5_ClientState" id="ctl00_ContentPlaceHolder1_ValidatorCalloutExtender5_ClientState" />                                                        
                           </div>
                      </div> 
                </div>
                 <div class="form-row row-fluid">
                      <div class="span12">
                          <div class="row-fluid">
                               <label class="form-label span4" for="tooltip">Last Name</label>
                               <input name="ctl00$ContentPlaceHolder1$txtLastEdit" type="text" id="ctl00_ContentPlaceHolder1_txtLastEdit" class="span8 tip" />  
                                   <span id="ctl00_ContentPlaceHolder1_RequiredFieldValidator5" style="color:Red;display:none;"></span>
            <input type="hidden" name="ctl00$ContentPlaceHolder1$ValidatorCalloutExtender6_ClientState" id="ctl00_ContentPlaceHolder1_ValidatorCalloutExtender6_ClientState" />                                                         
                           </div>
                      </div> 
                </div>
             <div class="form-row row-fluid">
                      <div class="span12">
                          <div class="row-fluid">
                               <label class="form-label span4" for="tooltip">Agent Percentage</label>
                                 <div class="span8 controls">
                             <select name="ctl00$ContentPlaceHolder1$ddlAgentPercEdit" id="ctl00_ContentPlaceHolder1_ddlAgentPercEdit">
    <option value="">Select Percentage</option>
    <option value="0.50">0.5</option>
    <option value="0.75">0.75</option>
    <option value="0.9">0.01</option>
    <option value="0.7">0.7</option>
    <option value="0.1">0.1</option>
    <option value="0.8">0.8</option>
    <option value="0.6">0.6</option>
    <option value="0.9">0.9</option>
    <option value="0.05">0.05</option>
    <option value="0.95">0.95</option>

</select>
                               </div>
                           </div>
                      </div> 
                </div>
            <input type="hidden" name="ctl00$ContentPlaceHolder1$hfLifeID" id="ctl00_ContentPlaceHolder1_hfLifeID" />
            </div>
        </div>
            </div>

你应该使用

$("#<%=ddlAgentPercEdit.ClientID%>").val('0.50');

检查我在JSFIDDLE上的示例

请从浏览器中粘贴渲染的html。 我会帮你解决它

我想这可能是个问题......

before the Modal您似乎正在设置下拉列表的值。

因此,从理论上讲,当您尝试设置值时, your dropdown is not yet visible

因此,在显示模态后尝试设置下拉列表的值。

 $('#editSplitAgent').modal('show');   // Show the modal first 
 $("[id*=ddlAgentPercEdit]").val('0.50');  // Then set the value  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM