get current text <select>

2019-02-27 04:43发布

问题:

                 <select runat="server" id="sctFieldMain">
                 <option></option>
                   </select>

Script

   $(xml).find("FieldMain").each(function()
 { 
    var value=$(this).find('Title').text()
     $('#<%=sctFieldMain.ClientID %>').
      append($("<option></option>").
      attr("value",value).
      text(value)); 
  });

 alert($("#<%=sctFieldMain.ClientID %> option:selected").text());

this retrun <%=sctFieldMain.ClientID %>

or

alert($("#<%=sctFieldMain.ClientID %> option:selected").val());

this retrun undefined

or

alert($("#<%=sctFieldMain.ClientID %> option:selected").html());

return null

i like get Cureent Text. that selected

回答1:

Use

alert($("#<%=sctFieldMain.ClientID %> option:selected").html());

EDITED

Also your question duplicates this one and this.



回答2:

You don't need the semi colon after the html() function and val does not return the html which is what your looking for.

alert($("#<%=sctFieldMain.ClientID %> option:selected").html());

However if you had the following and wanted the value "Example" then you should use val()

<option value="example">some test</option>