【问题标题】:get current text <select>获取当前文本 <select>
【发布时间】:2011-03-14 11:11:38
【问题描述】:
                 <select runat="server" id="sctFieldMain">
                 <option></option>
                   </select>

脚本

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

此返回

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

这个返回未定义

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

返回空值

我喜欢获取当前文本。被选中的

【问题讨论】:

  • 你不需要在你的 val() 或 text() BTW 末尾的分号
  • 你试过了吗:$('#').val();
  • 可能是个愚蠢的问题,但是您的 js 是否有可能在外部 .js 文件中而不是在页面上?

标签: jquery-ui jquery jquery-selectors


【解决方案1】:

使用

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

已编辑

您的问题也重复了this onethis

【讨论】:

  • @4111:您必须先做一个简单的检查,以确保列表中至少有一个选项。那么它不能返回null。
【解决方案2】:

在 html() 函数之后不需要分号,并且 val 不会返回您要查找的 html。

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

但是,如果您有以下内容并且想要值“示例”,那么您应该使用 val()

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

【讨论】:

  • qestion =$("select#sctQuestion option:selected").html();警报(问题);返回空
  • 你为什么引用:select#sctQuestion?
猜你喜欢
  • 1970-01-01
  • 2017-06-10
  • 1970-01-01
  • 2011-05-12
  • 2016-02-02
  • 1970-01-01
  • 2015-05-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多