根据下拉列表中的选择将预定义值插入文本框



我有一个简单的下拉列表,有 3 个选项可供选择,下面有一个文本框。用户选择一个选项后,我需要将其值作为文本插入文本框 - 如果客户选择"员工 1","TextBoxEmployee"将填充"1",依此类推。

<asp:DropDownList runat="server" ID="Assigned_Employee">
    <asp:ListItem Value="1" Text="Employee 1">Employee 1</asp:ListItem>
    <asp:ListItem Value="2" Text="Employee 2">Employee 2</asp:ListItem>
    <asp:ListItem Value="3" Text="Employee 3">Employee 3</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="TextBoxEmployee" runat="server"/>

你能建议最简单的方法吗?我可以使用 jQuery 吗?感谢您的帮助:)

请找到这个小提琴,它会帮助你

添加文档就绪功能,您可以在其中根据下拉选择设置预定义的文本框值,而页面加载和更改功能将更改文本框的值

$(function(){
  $("#TextBoxEmployee").val($(".changeclass").val())
})
$(".changeclass").on("change",function()
{
   $("#TextBoxEmployee").val($(this).val())
});

对于这样的事情,您可以选择本机js作为:

var sel = document.querySelector('#<%= TextBoxEmployee.ClientID %>'),
    inp = document.querySelector('#<%= Assigned_Employee.ClientID %>');
sel.addEventListener('change', function(e){
   inp.value = this.value;
});

最新更新