Get Text Select That Runat=“Server”

2019-03-06 00:15发布

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

2条回答
不美不萌又怎样
2楼-- · 2019-03-06 01:09

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>
查看更多
啃猪蹄的小仙女
3楼-- · 2019-03-06 01:17

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
查看更多
登录 后发表回答