[英]How do I pass selected values from a dropdown list to differents textboxes?
I have a dropdown list and some textboxes (5). 我有一个下拉列表和一些文本框(5)。 I would like to pass each selected item to respectively textbox1, textbox2... and so on.
我想将每个选定的项目分别传递给textbox1,textbox2 ...等。 How can I achieve this in either C# or jquery?
如何在C#或jquery中实现呢?
Thanks 谢谢
Use SelectedIndexChanged . 使用SelectedIndexChanged 。
Something like this: 像这样:
Code Front: 代码前:
<asp:DropDownList runat="server" id="MyDropDown" AutoPostBack="True" OnSelectedIndexChanged="MyDropDown_SelectedIndexChanged">
...
</asp:DropDownList>
Code Behind: 背后的代码:
void MyDropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
var selectedValue = ((DropDownList)sender).SelectedValue;
if (!string.IsNullOrEmpty(textbox1.Text))
textbox1.Text = selectedValue;
else if (!string.IsNullOrEmpty(textbox2.Text))
textbox2.Text = selectedValue;
...
}
i = 0
function setTextInTextField()
{
document.getElementById('textfield' + i).value = document.getElementById('dropdownlist').value;
i += 1;
}
You can use jQuery to do so: 您可以使用jQuery来做到这一点:
<asp:DropDownList id="MyDropDown" runat="server" ClientIDMode="Static"/>
<asp:TextBox id="MyTextBox" runat="server" ClientIDMode="Static"/>
$(document).ready(function(){
$('select#"MyDropDown").change(function(){
$('input#"MyTextBox").val($(this).val());
});
});
Please take a look at the following url: 请查看以下网址:
http://api.jquery.com/val/ http://api.jquery.com/val/
Hope this helps you out! 希望这可以帮助你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.