我们可以使用jquery“动态选项值”获取选择菜单的选择菜单

时间:2016-09-27 12:41:06

标签: javascript php jquery ajax

我想显示掉落值文字,但事情是这些值是动态的,接近的正确方法是什么,现在只有 id 值才会出现。

<div class="form-group">
    <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>
</div> 

和脚本:

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

我想在此处显示下拉列表选择文字:

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

2 个答案:

答案 0 :(得分:3)

请尝试以下代码:

//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 :(得分:1)

你可以试试这个

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

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

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