(1)select 下拉框
前端: <select name="gameserverlist" id="gameserverlist" runat="server" ></select>
<button onclick="btnFun()">测试</button>
js获取当前选中值:
function btnFun() { var obj = document.getElementById("gameserverlist"); //定位id var index = obj.selectedIndex; // 选中索引 var text = obj.options[index].text; // 选中文本 var value = obj.options[index].value; // 选中值 alert(text+"["+value+"]"); }
后端:
HtmlSelect select = (HtmlSelect)FindControl("gameserverlist"); if (!IsPostBack) { DataTable table = new DataTable(); table = this.GetGameServerListData(); if (table != null) { ListItem itemSon = null; string text="", val = ""; foreach (DataRow row in table.Rows) { text = row["server_name"] == null ? "" : row["server_name"].ToString(); val = row["server_id"] == null ? "" : row["server_id"].ToString(); itemSon = new ListItem(text,val); select.Items.Add(itemSon); } } }