2012-02-07 95 views
0

我有一個下拉列表和一些文本框(5)。我想將每個選定的項目分別傳遞給textbox1,textbox2 ...等等。我如何在C#或jQuery中實現這一點?如何將選定值從下拉列表傳遞到不同文本框?

感謝

+1

它是一個多選擇? – Rafay 2012-02-07 12:03:19

+0

需要更多信息。一個[jsfiddle](http://www.jsfiddle.net/)會有幫助 – WickyNilliams 2012-02-07 12:06:14

回答

0

使用SelectedIndexChanged

事情是這樣的:

代碼前面:

<asp:DropDownList runat="server" id="MyDropDown" AutoPostBack="True" OnSelectedIndexChanged="MyDropDown_SelectedIndexChanged"> 
    ... 
</asp:DropDownList> 

代碼背後:

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; 
    ... 
} 
+0

這段代碼將相同的選定值傳遞給所有文本框:(它應該將選定的值傳遞給定義文本框,即:selecteditem1去textbox1 ... – Totem 2012-02-09 14:21:27

+0

你只需要檢查textbox1是否在文本屬性中有任何值,然後將selecteditem分配給textbox2等請參閱代碼更改 – 2012-02-09 17:25:04

+0

謝謝,這就是缺少的東西 – Totem 2012-02-09 19:36:17

0
i = 0 

function setTextInTextField() 
{ 
    document.getElementById('textfield' + i).value = document.getElementById('dropdownlist').value; 
    i += 1; 
} 
0

您可以使用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()); 
    }); 
}); 

請看看以下網址:

http://api.jquery.com/val/

希望這可以幫助你!

+0

謝謝,但你的答案適用於1個文本框什麼與超過1個文本框,其中每個選定的項目被傳遞到不同的文本框,即:selectedItem1到textbox1,selectedItem2到textbox2 ... – Totem 2012-02-07 16:55:12

+0

看看這個例子,應該做的竅門: http://api.jquery.com/each/ – 2012-02-07 17:39:37

相關問題