獲取選中項的Value值: $('select#sel option:selected').val(); 或者 $('select#sel').find('option:selected').val(); 獲取選中項的Text值: $('select#seloption:selected').text(); 或者 $('select#sel').find('option:selected').text();
$('select#sel').get(0).selectedIndex;
$('select#sel option:last').attr("index")
$('select#sel')[0].options.length; 或者 $('select#sel').get(0).options.length;
$('select#sel option:first').attr('selected','true') 或者 $('select#sel')[0].selectedIndex = 0;
$('select#sel option:last).attr('selected','true')
$('select#sel')[0].selectedIndex =索引值;索引值=0,1,2....
$('select#sel').attr('value','4'); 或者 $("select#sel option[value='4']").attr('selected', 'true');
$("select#sel option[value='3']").remove();
$(" select#sel option ").eq(索引值).remove();索引值=0,1,2.... 如刪除第3個Radio: $(" select#sel option ").eq(2).remove();
$(" select#sel option ").eq(0).remove(); 或者 $("select#sel option:first").remove();
$("select#sel option:last").remove();
$("select#sel").remove();
$("select#sel").append("f");
$("select#sel").prepend("0");
$(' select#sel option ').each(function (index, domEle) { //寫入代碼 });.net控件dropdownlist動態(tài)綁定數據
SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
string strSQL = "select * from CompanyType";
SqlDataAdapter ada = new SqlDataAdapter(strSQL, conn);
DataSet ds = new DataSet();
ada.Fill(ds, "CompanyType");
DropDownList1.DataSource = ds.Tables["CompanyType"].DefaultView;
DropDownList1.DataValueField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataTextField = ds.Tables["CompanyType"].Columns[1].ColumnName;
DropDownList1.DataBind();
ds.Dispose();
第一種方法:protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection conn = UtilitySqlClass.OperateDataBase.ReturnConn();
try
{
conn.Open();
this.DropDownList1.Items.Add("");
string strSQL = "select CompanyType from CompanyType";
SqlCommand com = new SqlCommand(strSQL, conn);
SqlDataReader dr = com.ExecuteReader();
while (dr.Read())
{
this.DropDownList1.Items.Add(dr["CompanyType"].ToString());
}
}
catch (Exception ex)
{
Response.Write("<scirpt>alert('" + ex.Message.ToString() + "')</script>");
}
finally
{
conn.Close();
}
}
}
string ConnString = ConfigurationSettings.AppSettings["ConnectionString"];
//創(chuàng)建一個SqlConnection
SqlConnection Conn = new SqlConnection( ConnString );
string SQL_Select = "select id, ItemName from DDLItem order by id desc";
//構造一個SqlDataAdapter
SqlDataAdapter myAdapter = new SqlDataAdapter( SQL_Select, Conn);
//開始讀取數據
Conn.Open();
DataSet dataSet = new DataSet();
myAdapter.Fill( dataSet,"Table1" );
Conn.Close();
//開始綁定DropDownList
//指定DropDownList使用的數據源
DropDownList1.DataSource = dataSet.Tables["Table1"].DefaultView;
//指定DropDownList使用的表里的那些字段
DropDownList1.DataTextField = "ItemName"; //dropdownlist的Text的字段
DropDownList1.DataValueField = "id";//dropdownlist的Value的字段
DropDownList1.DataBind();
con.Open();
SqlCommand cmd = new SqlCommand(strSql,con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList1.Items.Add(new ListItem(dr["status"].ToString(), dr["status_Id"].ToString()));
}
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網友會為你解答??! 點擊進入論壇