想用腳本控制一個dropdownlist.讓它顯示指定的值。。
可弄了半天就是沒出來。。
<asp:DropDownList CssClass="dropdownlist" ID="cmbSalesman" runat="server" Width="210px">
</asp:DropDownList></td>document.getElementById("<%=cmbSalesman.ClientID %>").SelectValue = "test";
document.getElementById("<%=cmbSalesman.ClientID %>").value = "test";其中。dropdownlist已經綁定數據,且包含test
但是就是不顯示選中test
而且,alert(document.getElementById("<%=cmbSalesman.ClientID %>").SelectValue);
顯示出來的就是test
可頁面就是不出來。。
可弄了半天就是沒出來。。
<asp:DropDownList CssClass="dropdownlist" ID="cmbSalesman" runat="server" Width="210px">
</asp:DropDownList></td>document.getElementById("<%=cmbSalesman.ClientID %>").SelectValue = "test";
document.getElementById("<%=cmbSalesman.ClientID %>").value = "test";其中。dropdownlist已經綁定數據,且包含test
但是就是不顯示選中test
而且,alert(document.getElementById("<%=cmbSalesman.ClientID %>").SelectValue);
顯示出來的就是test
可頁面就是不出來。。
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="Scripts/jquery-1.4.1.min.js"></script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="test" Value="0"></asp:ListItem>
<asp:ListItem Text="test2" Value="20"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
<script type="text/javascript">
$(function () {
$("#DropDownList1")[0][1].selected = true;
});
</script>
</body>
</html>
<option value="demo">!demo!</option>
<option value="test">!test!</option>
</select><input type="button" value="test" onclick="document.getElementById('idSelect').value='test';"/>1、确定执行时option都加载了;
alert(document.getElementById("<%=cmbSalesman.ClientID %>").options.length);2、和option的value一致,注意不是和text一致。
是dropdownlist.不是 select.....
//添加值
$("#DropDownList1").append("<option value=\"1\">test</option>");
//设置为显示的值
$("#DropDownList1 option[text='test']").attr("selected", true);