简体   繁体   English

如何将下拉列表中的选定值传递给差异文本框?

[英]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.

相关问题 相对于在下拉框中选择的项目ID,如何将文本框的值(IN LOOP)从视图传递到控制器? - How to pass the values of the textboxes(which are IN LOOP) from view to the controller with respect to the item id selected in the dropdown box? 如何从 Select2 下拉列表中获取选定值列表并传递给 controller? - How to obtain list of selected values from Select2 dropdown list and pass to controller? 如何将值从Datagridview选定的行复制到文本框 - How to Copy values from a Datagridview selected row to textboxes 在ajax中传递differents值 - Pass differents values in ajax 如何将文本框列表与并行列表进行比较? - How do I compare a list of textboxes to a list in parallel? 将选定的下拉列表值从View传递到Controller - pass selected dropdown list value from View to Controller 我有多个“数量”文本框,如何发布所有文本框的值 - I have multiple “quantity” textboxes, how do I post values of all textboxes 如何将选定的项目ID从下拉列表传递给控制器​​的操作? - How to pass selected item id from dropdown list to controller's action? 如何用最少的代码将许多文本框中的值插入数据库? - How do I insert values from many textboxes into my database with minimal code? 如何使用C#将SQL数据库中的值获取到文本框中? - How do I get values from a SQL database into textboxes using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM