如何使用jquery在下拉列表中显示所选值

时间:2015-12-29 04:58:00

标签: jquery html

我想在下拉列表中显示所选名称, 这是我的观看代码

<tr data-view-key="<?php echo $jmf_id; ?>">
<td class="jnl_id"><?php echo $jmf_jnl_id; ?></td>
<td class="question"><?php echo $jmf_question; ?></td> 
<td class="question_type">
    <?php 
    if($jmf_question_type == 'ss')
    {
    echo 'Single select (checkbox)';
    }
    else if($jmf_question_type == 'ms')
    {
    echo 'Multi select (checkbox)';
    }
    else if($jmf_question_type == 'mo')
    {
    echo 'Multi option (radio)';
    }
    else if($jmf_question_type == 'ft')
    {
    echo 'Free textarea';
    }
    ?>
</td>
<td class="question_option"><?php echo $jmf_question_options; ?></td>
<td>
    <a href="#<?php echo $jmf_id; ?>" name="edit_meta" ><span class="glyphicon glyphicon-edit" title="Edit"></span></a>&nbsp;&nbsp;
    <a href="#<?php echo $jmf_id; ?>" name="delete_meta" ><span class="glyphicon glyphicon-trash" title="Delete"></span></a>
</td>

Jquery代码是

$(document).on("click", "a[name = 'edit_meta']", function(event)
{
    event.preventDefault();
        $("a[name='delete_meta']").confirmation("hide");
        var jmf_id = $(this).attr("href").substring(1);
        var parent = $(this).parents("[data-view-key='"+jmf_id+"']");
        var jnl_id = parent.find("td.jnl_id").text();
        var question = parent.find("td.question").text();
        var question_type = parent.find("td.question_type").text();//output eg: Multi select (checkbox)
        var question_option = parent.find("td.question_option").text();
        $("#hdn_mf_id").val(jmf_id);
        $("#question").val(question);
       --> $("#question_type").val(question_type);//want to display the selected list item here(not value).

        $("#question_options").val(question_option);


});

我想在下拉列表中显示列出的项目名称,可以使用哪个功能代替.val()

1 个答案:

答案 0 :(得分:0)

您需要使用:selected目标定位所选选项并获取其属性'name'

$("#question_type option:selected").attr('name')

示例:http://jsfiddle.net/3kgbG/1240/

如果您希望从下拉列表中显示所选文本

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

例子; http://jsfiddle.net/DinoMyte/3kgbG/1241/