关于javascript:我们可以使用jquery“动态选项值”获取所选选项选择菜单的文本

Can we get the slected options Text of select menu using jquery “Dynamic option values”

本问题已经有最佳答案,请猛点这里访问。

我想显示DropValue文本,但问题是这些值是动态的,正确的方法是什么,现在只有ID值出现。

1
2
3
4
5
6
7
    <select class="wp-form-control" id="joblocation" name="joblocation">
        <option value="0">Select Job Location</option>
        <?php  $City = $conn->query("SELECT * FROM tbl_cities  ORDER BY city_pid ASC");
            while ($Cityresult = $City->fetch_assoc()) {?>
            <option value="<?php echo $Cityresult['city_pid']; ?>"> <?php echo $Cityresult['city_name']; ?> </option>
        <?php } ?>
    </select>

脚本:

1
2
var joblocation = $('#joblocation').find(":selected").text();
$("#joblocation1").html(joblocation);

我想在这里显示下拉选择的文本:

1
<span id="joblocation1"> </span>


尝试以下代码:

1
2
3
4
//This is for debugging and will show you the selected option object in console
console.info($("#joblocation option:selected") );

$("#joblocation1").html($("#joblocation option:selected").text() );

你可以试试这个

1
 var optionText = $("#joblocation option:selected").text() //gets you the text from the selected option

然后将变量添加到跨度中,如下所示:

1
$("#joblocation1").text(optionText);