问题
<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.
- test that this returns the correct id.
<%=Select1.ClientID %>
which should beSelect1
- 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)
oralert(value)
if you haven't evolved yet :) 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