简体   繁体   English

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

[英]the value of option selected doesn't show in the text box

I create a select box with a search box by jquery chosen and I fill it by data from database and I want to show the value of option selected in the text box but when choose an option, nothing show text box so what is the problem and what can I do? 我创建了一个选择框,其中有一个通过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 code 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# code 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--");
txt4.value = sel4.value;

sel4.value; is the value of the whole selection box, which is likely a list or array. 是整个选择框的值,很可能是列表或数组。

it should be something like sel4.Items.Selected.value; 它应该类似于sel4.Items.Selected.value; or 'sel4.Selected.value` 或“ sel4.Selected.value”

Try the below code, to assign the selected drop-down value to the text-box. 尝试以下代码,以将选定的下拉值分配给文本框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM