日期:2014-05-18  浏览次数:20897 次

怎样用js为dropdownlist赋值
<asp:DropDownList style="float:left;" ID="DDL_ShowCompanyName" runat="server" OnSelectedIndexChanged="DDL_NewsTName_SelectedIndexChanged"
  AutoPostBack="True">
  </asp:DropDownList>
怎样用JS把DDL_ShowCompanyName的selectedvalue赋值给下一个DropDownList
<asp:DropDownList ID="DDL_CompanyName" runat="server" Width="155px">
  </asp:DropDownList>

------解决方案--------------------
服务器控件最好在服务端赋值。
如果要在客户端做,要拿到id,如果不用模板,则:
var dpl1 = document.getElementById('DDL_ShowCompanyName');
var dpl2 = document.getElementById('DDL_CompanyName').options;

for (var i = 0; i < dpl2.length; i++) {
if (dpl2.options[i] === dpl1.options[dpl1.selectedIndex]) {
dpl2.options[i].selected = true;
break;
}
}

用jquery,写法较简洁:

$('#DDL_CompanyName').val($('#DDL_ShowCompanyName').val());