Get Text Select That Runat=“Server”

橙三吉。 提交于 2019-12-20 06:15:09

问题


   <select   id="Select1">
         <option>1</option>
         <option>2</option>
         <option>3</option>
  </select>

alert($("#Select1").val());

this code Is Correct And Return Current Value.

BUT

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

function parseXmlQuestion(xml)
{

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

}

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

or

alert( $('#<%=Select1.ClientID %>').text());
alert( $('#<%=Select1.ClientID %> option:selected').val());

or

alert( $('#<%=Select1.ClientID %>').html());
alert( $('#<%=Select1.ClientID %> option:selected').val());

return null or undefined .


回答1:


Ok you are a bit weird and you are helping us less each time you answer...

Here is what you should do.

  1. test that this returns the correct id. <%=Select1.ClientID %> which should be Select1
  2. check your xml, that is, correct, (use json preferably it's far easier) including the caps of your xml tags. so do either console.log(value) or alert(value) if you haven't evolved yet :)
  3. parseXmlQuestion() should be called somewhere on the page otherwise you just having a sitting duck not being shot or not telling it to run in other words



回答2:


u need to remove <option></option> from

   <select runat="server"   id="Select1">
         <option></option> <-- your values get added after this so probably the null is coming from here
  </select>

it should be like

   <select runat="server"   id="Select1"></select>


来源:https://stackoverflow.com/questions/5298203/get-text-select-that-runat-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!