我有一個下拉列表和一些文本框(5)。我想將每個選定的項目分別傳遞給textbox1,textbox2 ...等等。我如何在C#或jQuery中實現這一點?如何將選定值從下拉列表傳遞到不同文本框?
感謝
我有一個下拉列表和一些文本框(5)。我想將每個選定的項目分別傳遞給textbox1,textbox2 ...等等。我如何在C#或jQuery中實現這一點?如何將選定值從下拉列表傳遞到不同文本框?
感謝
事情是這樣的:
代碼前面:
<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;
...
}
i = 0
function setTextInTextField()
{
document.getElementById('textfield' + i).value = document.getElementById('dropdownlist').value;
i += 1;
}
您可以使用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());
});
});
請看看以下網址:
希望這可以幫助你!
謝謝,但你的答案適用於1個文本框什麼與超過1個文本框,其中每個選定的項目被傳遞到不同的文本框,即:selectedItem1到textbox1,selectedItem2到textbox2 ... – Totem 2012-02-07 16:55:12
看看這個例子,應該做的竅門: http://api.jquery.com/each/ – 2012-02-07 17:39:37
它是一個多選擇? – Rafay 2012-02-07 12:03:19
需要更多信息。一個[jsfiddle](http://www.jsfiddle.net/)會有幫助 – WickyNilliams 2012-02-07 12:06:14