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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16594938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:16:13  来源:igfitidea点击:

set selected value of a asp dropdown with jquery

javascriptjqueryasp.netjquery-uidrop-down-menu

提问by Laziale

I am trying to set the dropdown value with jquery when modal popup is displayed. I am trying with some static data but without any success. This is what I have:

当显示模式弹出窗口时,我试图用 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');
          });

The dropdown has this values:

下拉列表具有以下值:

  <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>

I have textbox and hidden field which have the correct value when the modal popup is displayed. As you can see in the jquery event I am trying static data just so I can see its working ok with static data, but originally I'll need to assign the splitPC var value to the dropdown value. Thanks in advance for your advices, Laziale

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

EDIT: rendered code for the modal popup:

编辑:模式弹出窗口的渲染代码:

        <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>

回答by Arif YILMAZ

you should use

你应该使用

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

check my example on JSFIDDLE

JSFIDDLE上查看我的示例

Please paste your rendered html from your browser. I will help you fix it

请从您的浏览器粘贴您呈现的 html。我会帮你修好

回答by Sushanth --

I think this might be an issue ...

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

You seem to be setting the value of the dropdown , before the Modalis being shown ..

您似乎正在设置下拉列表的值,before the Modal正在显示..

So theoretically speaking your dropdown is not yet visiblewhen you are trying to set the value.

所以从理论上讲,your dropdown is not yet visible当您尝试设置该值时。

So try setting the value of the dropdown after the modal is shown..

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

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