【问题标题】:Get the value of select tag jQuery [duplicate]获取选择标签jQuery的值[重复]
【发布时间】:2013-07-15 13:42:40
【问题描述】:

我有一个类似的代码

<select id="sel1">
    <option label="l1" value="1">Cool</option>
    <option label="l2" value="2">Hot</option>
</select>

现在我想在更改 jQuery 中选定项目的值时获取值应该是 Cool 或 Hot。 我正在尝试querySource = $("#querySource").val();,但这显示为12

怎么做?

【问题讨论】:

  • alert($("#sel1 option:selected").text());
  • 非常感谢 Mr_Green,它工作正常。
  • 请使用此代码获取值 $('#sel1 option:selected').text();

标签: jquery


【解决方案1】:

请用这个:

如果你想获取文本值:喜欢 Cool and Hot 然后使用这个

$('#sel1 option:selected').text();

如果你想获得价值:像 1,2 那么

$('#sel1 option:selected').val();

【讨论】:

    【解决方案2】:

    通过.val(),您将收到 value 属性的值。您可以通过.text()获取文本。

    在您选择的选项上使用此方法!见this answer!

    【讨论】:

    • 但如果我使用 .text() 来执行此操作,那么它会显示整个 不应预期
    【解决方案3】:

    使用option:selected

    querySource = $("#querySource option:selected").text();
    

    你也可以使用下面的

    $("#querySource option").is("selected").text()
    $("#querySource").children("option").is("selected").text()
    

    【讨论】:

      【解决方案4】:

      试试这个

      $("#sel1 option:selected").text()
      

      【讨论】:

        【解决方案5】:

        你可以用这个

        $("#querySource option:selected").text();
        

        【讨论】:

          猜你喜欢
          • 2013-11-23
          • 1970-01-01
          • 2019-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-03-22
          • 1970-01-01
          • 2012-08-06
          相关资源
          最近更新 更多