所选选项的值不会显示在文本框中

时间:2018-10-06 15:08:24

标签: javascript c# jquery asp.net

我通过选择jquery创建一个带有搜索框的选择框,并用数据库中的数据填充它,我想显示在文本框中选择的选项的值,但是当选择一个选项时,什么也不显示文本框,所以问题,我该怎么办?

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.5.1/chosen.jquery.min.js"></script>      

html js代码

<div class="span-inside">
    <select runat="server" class="chosen"  
            id="hospSelect"  style="width:350px;">                                 
    </select>
    <input runat="server" type="text" id="hospID" />
    <script type="text/javascript">
        $(".chosen").chosen();
            var txt4 = document.getElementById("hospID");
            var sel4 = document.getElementById("hospSelect");
            sel4.addEventListener("change", function () {
            txt4.value = sel4.value;
        })
    </script>
</div>

c#代码

DataBaseConnection db5 = new DataBaseConnection();
db5.disconnect();
string sql5 = "select Hospital_ID , Hospital_Name From TBL_Hospitals";
SqlCommand cmd5 = new SqlCommand(sql5, db5.connect());
hospSelect.DataSource = cmd5.ExecuteReader();

hospSelect.DataTextField = "Hospital_Name";
hospSelect.DataValueField = "Hospital_ID";

hospSelect.DataBind();
hospSelect.Items.Insert(0, "--Choose Hospital--");

2 个答案:

答案 0 :(得分:0)

txt4.value = sel4.value;

sel4.value;是整个选择框的值,很可能是列表或数组。

它应该类似于sel4.Items.Selected.value;或'sel4.Selected.value`

答案 1 :(得分:0)

尝试以下代码,将选定的下拉值分配给文本框。

$("select.chosen").change(function(){
        var selectedhosp = $(".chosen option:selected").val();
        $('#hospID').val(selectedhosp);
    });