2011-03-14 67 views
0
<select id="Select1"> 
     <option>1</option> 
     <option>2</option> 
     <option>3</option> 
    </select> 

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

此代码是正确的并返回当前值。获取文本选择Runat =“服务器”

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()); 

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

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

返回NU或不确定。

+0

它实际上是追加'option'元素吗?当你在客户端(像FireBug那样的东西)检查它时,究竟发生了什么?第二个例子添加了更多的变量,而不仅仅是'runat =“server”'。 – David 2011-03-14 12:02:45

+0

'<%= Select1.ClientID%>'应该返回什么? – Val 2011-03-14 12:08:19

+1

你为什么重新发布这个问题? http://stackoverflow.com/questions/5297778/get-current-text-select – Andrew 2011-03-14 12:10:34

回答

0

好吧你是有点不可思议,你是帮助我们每次回答时间不...

这是你应该做的。

  1. 测试这将返回正确的ID。 <%=Select1.ClientID %>这应该是Select1
  2. 检查你的xml,也就是正确的,(包括你的xml标签的上限,使用json最好更容易)。这样做既console.log(value)alert(value)如果您还没有进化:)
  3. parseXmlQuestion()应某处的页面,否则你只是有一个坐在鸭不被枪杀或者不告诉它,换句话说运行被称为
0

u需要从

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

删除​​3210应该像

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

如果删除

+0

从哪里删除'选项'? – Rafay 2011-03-14 12:58:55

+0

在html代码中,在脚本中remoove错误说附加没有定义 – ashkufaraz 2011-03-14 13:00:19