JavaScript與DropDownList 區(qū)別分析
代碼
<asp:DropDownList ID = "ddlCities" runat = "server">
<asp:ListItem Value = "0">長(zhǎng)沙</asp:ListItem>
<asp:ListItem Value = "1">北京</asp:ListItem>
<asp:ListItem Value = "2">天津</asp:ListItem>
<asp:ListItem Value = "3">漠河</asp:ListItem>
</asp:DropDownList>
<select id = "ddlNames" runat ="server">
<option value = "0">James</option>
<option value = "1">Green</option>
<option value = "2">Lily</option>
<option value = "3">Lucy</option>
</select>
在瀏覽器中查看該頁(yè)面,并點(diǎn)擊查看源文件,不難看出,asp.net頁(yè)面被渲染成了如下格式:
代碼
<select name="ddlCities" id="ddlCitys">
<option value="0">長(zhǎng)沙</option>
<option value="1">北京</option>
<option value="2">天津</option>
<option value="3">漠河</option>
</select>
<select name="ddlNames" id="ddlNames">
<option value="0">James</option>
<option value="1">Green</option>
<option value="2">Lily</option>
<option value="3">Lucy</option>
</select>
好了,接下來(lái)介紹一下要用javascript操縱DropDownList控件,首先得了解select(或者DropDownList)的兩個(gè)最基本的屬性,一個(gè)是value屬性,一個(gè)是text屬性,還有一個(gè)selectedIndex屬性,用來(lái)標(biāo)識(shí)當(dāng)前選中的項(xiàng)(數(shù)字),具體可參見(jiàn)上面的示例代碼。
下面正式言歸正傳,主要介紹如下幾點(diǎn):
(1) 清空DropDownList控件中的值。
document.getElementById('ddlCities').options.length = 0;
(2) 判斷DropDownList中是否有value為'Param1'的ListItem。
function isListItemExist(objDdl , objItemValue)
{
var isExist = false;
for(var i in objSelect.options)
{
if(i.value == objItemValue)
{
isExist = true;
break;
}
}
return isExist;
}
JavaScript與DropDownList
服務(wù)器控件DropDownList和Javascript的之間的傳遞
<script language="javascript">
function hehe()
{
document.all('txtSdept').value =document.all('ddlSdept').options[document.all('ddlSdept').selectedIndex].text;
}
</script>
<asp:DropDownList id="ddlSdept" style="Z-INDEX: 101; LEFT: 240px; POSITION: absolute; TOP: 112px" onchange="hehe()" runat="server">
<asp:ListItem Value="1">計(jì)算機(jī)系</asp:ListItem>
<asp:ListItem Value="2">機(jī)械系</asp:ListItem>
<asp:ListItem Value="3">電子系</asp:ListItem>
<asp:ListItem Value="4">英語(yǔ)系</asp:ListItem>
<asp:ListItem Value="5">中文系</asp:ListItem>
</asp:DropDownList>
<asp:TextBox id="txtSdept" style="Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 112px" runat="server"></asp:TextBox>
參考文章:
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無(wú)標(biāo)題文檔</title>
<script language="javascript">
function moveSelected(select, down)
{
if (select.selectedIndex != -1)
{
if (down)
{
if (select.selectedIndex != select.options.length - 1)
var i = select.selectedIndex + 1;
else
return;
}
else
{
if (select.selectedIndex != 0)
var i = select.selectedIndex - 1;
else
return;
}
var swapOption = new Object();
swapOption.text = select.options[select.selectedIndex].text;
swapOption.value = select.options[select.selectedIndex].value;
swapOption.selected = select.options[select.selectedIndex].selected;
swapOption.defaultSelected = select.options[select.selectedIndex].defaultSelected;
for (var property in swapOption)
select.options[select.selectedIndex][property] = select.options[property];
for (var property in swapOption)
select.options[property] = swapOption[property];
}
}
<form id="formName" name="formName" >
<select name="selectName" id="selectName" size="8">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
</select>
<input id="moveUp" onclick="moveSelected(this.form.selectName, false)" type="button" value="上移" />
<input id="moveDown" onclick="moveSelected(this.form.selectName, false)" type="button" value="下移" />
</form>
1、js腳本如何訪問(wèn)服務(wù)器控件的值
界面上有一個(gè)TextBox控件,ID為Name,js里可以采用如下腳本取Name的值
var myvalue=document.all('Name').value;
2、服務(wù)器控件如何取js中變量的值
目前未發(fā)現(xiàn)比較好的辦法,我通常采用的方法是在界面上放一個(gè)隱藏的控件HtmlInputHidden,然后設(shè)置為以服務(wù)器控件運(yùn)行,這樣在js腳本中和ASP.NET代碼里都可以訪問(wèn)到該控件的值
js中給服務(wù)器控件賦值:
var bt=document.all('Name').value;
bt.value='名稱';
ASP.NET中使用Name.Value來(lái)訪問(wèn)。
3、如何遍歷界面上所有TextBox元素
var inputList = document.body.getElementsByTagName("INPUT");
for(var i=0;i<inputList.length;i++)
{
if(inputList.disabled==false && (inputList.type=='text' || inputList.type=='password'))
{
inputList.value="";
}
}
4、讓dropdownlist選擇到指定項(xiàng)
選擇dropdownlist中值為“我得選擇”得項(xiàng)
var handl=document.all('List1');
var my_value='我得選擇';
for(var index=0;index<handle.options.length;index++)
{
if(handle.options[index].text==my_value)
{
handle.selectedIndex=index;
}
}
JS取消ListBox,Select,DropDownList選項(xiàng)的選中
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
<asp:ListItem Text="4" Value="4"></asp:ListItem>
<asp:ListItem Text="5" Value="5"></asp:ListItem>
</asp:ListBox>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("#cel").click(function(){
$("#<%=ListBox1.ClientID%>").get(0).selectedIndex=-1;
});
});
</script>
<div id="cel" style="cursor:pointer;">取消綁定</div>
dropdownlist 選中值
c#:
ddlProvince.SelectedIndex = ddlProvince.Items.IndexOf(ddlProvince.Items.FindByText( "浙江"));
javascript:
var requiredSdept=$("select[@id='ddlSdept'] option[@selected]").val();
var requiredSdept = $("#ddlSdept option[@selected]").text();
var select1 = document.all.<%= ddlSdept.ClientID %>;
var select1value = select1.options[select1.selectedIndex].value;
var select1Text = select1.options[select1.selectedIndex].innerText;
其中select1Text 為選中的值。如果在模態(tài)窗口中使用,可以用如下代碼:
window.returnValue=select1Text;//這是返回給父窗體的值
window.close();
javascript中設(shè)定dropdownlist哪一項(xiàng)為當(dāng)前選中項(xiàng)
方法1:
i = 2
document.all.dropdownlistID.options[i].selected=true
方法2:
obj.selectedIndex = 2;
方法3:
obj.value="你要設(shè)的數(shù)值。"http://Dropdownlist就會(huì)自動(dòng)把那個(gè)值設(shè)為當(dāng)前。
javascript清除dropdownlist的項(xiàng)
//清除原有項(xiàng)
function clearitem(){
var drp1 = document.getElementById("drp1");
while(drp1.options.length>0)
{
drp1.options.remove(0);
}
}
動(dòng)態(tài)更改方法(根據(jù)城市代碼取得該市商業(yè)區(qū)并添加到DropDownList中)
function getsyq()
{
var city = document.getElementById("DropDownList_Cities").value; //取得城市代碼
var htp = new ActiveXObject("Msxml2.XMLHTTP");
var drp1 = document.getElementById("drp1");
var url = "?stat=1&city="+city
htp.open("post",url,true)
htp.onreadystatechange=function()
{
if(htp.readyState==4)
{
clearitem(); //清除原有下拉項(xiàng)
var str = htp.responseText;
var opt = str.split(',');
var s = opt.length
for(var j = 0;j<s;j++)
{
var newOption = document.createElement("OPTION"); //定義一個(gè)新的項(xiàng)
var ff = opt[j].split('|');
newOption.text = ff[1];
newOption.value = ff[1];
drp1.options.add(newOption);
}
}
}
htp.send()
}
JavaScript實(shí)現(xiàn)DropDownList(Select)三級(jí)聯(lián)動(dòng)無(wú)刷新
<script language=javascript>
function CountryChange(){
countryid=document.getElementById("ddlContry").value;
if(countryid==null||countryid==""){
alert("請(qǐng)選擇所屬國(guó)家");
CountryDel("ddlProvince");//清空DropDownList
CountryDel("ddlCity");//清空DropDownList
return false;
}
var countrystring="";
var posturl=location.protocol+"http://"+location.hostname+"http://soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid;
countrystring=openUrl(posturl);
if(countrystring=="-2"){//查詢失敗
alert("數(shù)據(jù)查詢失敗");
return false;
}
//分割并寫(xiě)入DropDownList
CountryDel("ddlProvince");//清空DropDownList
CountryDel("ddlCity");//清空DropDownList
if(countrystring==""){
return false;
}
var stringarray=countrystring.split("|");
for(var i=0;i<stringarray.length;i++){//重寫(xiě)DropDownList
//拆分內(nèi)部數(shù)組
var optionarray=stringarray[i].split(",");
var newoption=document.createElement("option");
newoption.text=optionarray[0];
newoption.value=optionarray[1];
document.getElementById("ddlProvince").options.add(newoption);
}
}
function CountryDel(AreaName){//清空DropDownList
var countnum=document.getElementById(AreaName).options.length;
for(var i=1;i<countnum;i++){//清空DropDownList
document.getElementById(AreaName).remove(countnum-i);
}
}
function ProvinceChange(){
countryid=document.getElementById("ddlProvince").value;
if(countryid==null||countryid==""){
alert("請(qǐng)選擇所屬省");
CountryDel("ddlCity");//清空DropDownList
return false;
}
var countrystring="";
var posturl=location.protocol+"http://"+location.hostname+"http://soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid;
countrystring=openUrl(posturl);
if(countrystring=="-2"){//查詢失敗
alert("數(shù)據(jù)查詢失敗");
return false;
}
//分割并寫(xiě)入DropDownList
CountryDel("ddlCity");//清空DropDownList
if(countrystring==""){
return false;
}
var stringarray=countrystring.split("|");
for(var i=0;i<stringarray.length;i++){//重寫(xiě)DropDownList
//拆分內(nèi)部數(shù)組
var optionarray=stringarray[i].split(",");
var newoption=document.createElement("option");
newoption.text=optionarray[0];
newoption.value=optionarray[1];
document.getElementById("ddlCity").options.add(newoption);
}
}
function openUrl(url)
{
var objxml=new ActiveXObject("Microsoft.XMLHttp")
objxml.open("GET",url,false);
objxml.send();
retInfo=objxml.responseText;
if (objxml.status=="200")
{
return retInfo;
}
else
{
return "-2";
}
}
</script>
Html代碼
<asp:DropDownList ID="ddlContry" runat="server" onchange="CountryChange()" OnSelectedIndexChanged="ddlContry_SelectedIndexChanged">
<asp:ListItem Value=" ">請(qǐng)選擇所屬國(guó)家</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlProvince" runat="server" onchange="ProvinceChange()" OnSelectedIndexChanged="ddlProvince_SelectedIndexChanged">
<asp:ListItem Value=" ">請(qǐng)選擇所屬省</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddlCity" runat="server">
<asp:ListItem Value=" ">請(qǐng)選擇所屬市</asp:ListItem>
</asp:DropDownList>
Aspx.cs代碼
protected void Page_Load(object sender, EventArgs e)
{
SoLe.Common.StringFormat sft = new SoLe.Common.StringFormat();
string AreaId = sft.Html_Fn(Request.QueryString["AreaId"].ToString());
StringBuilder AreaString = new StringBuilder();
AreaString.Append("");
if (!Page.IsPostBack)
{
//Response.Write(AreaIdValid(AreaId.ToString()));
SoLe.BLL.AreaTable bll = new SoLe.BLL.AreaTable();
DataSet ds = new DataSet();
ds = bll.GetList(" PaterId=" + AreaId.ToString()+" ");
if (!object.Equals(ds, null) && ds.Tables[0].Rows.Count > 0) {
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) {
if (string.IsNullOrEmpty(AreaString.ToString()))
{
AreaString.Append(ds.Tables[0].Rows[i]["Title"].ToString() + "," + ds.Tables[0].Rows[i]["Id"].ToString());
}
else {
AreaString.Append("|" + ds.Tables[0].Rows[i]["Title"].ToString() + "," + ds.Tables[0].Rows[i]["Id"].ToString());
}
}
}
}
Response.Write(AreaString.ToString());
}
JavaScript分割字符串裝載到DropDownList
假設(shè)變量str存放著幾組對(duì)應(yīng)的數(shù)據(jù),DropDownList名為ddlType,把字符分隔后的數(shù)據(jù)裝載到ddlType,具體代碼如下:
程序代碼
<script language="javascript">
function LoadType() {
var str = "1|網(wǎng)頁(yè)|2|圖片|3|企業(yè)|4|資訊|";
var temp = str.split("|");
var count = (temp.length - 1) / 2;
for (var i = 0; i <= count; i++) {
document.all("ddlType").options.add(new Option(temp[i], temp[i + 1]));
}
return;
}
<script>
- JS簡(jiǎn)單操作select和dropdownlist實(shí)例
- JS,Jquery獲取select,dropdownlist,checkbox下拉列表框的值(示例代碼)
- JS獲取DropDownList的value值與text值的示例代碼
- js給dropdownlist添加選項(xiàng)的小例子
- 用javascript為DropDownList控件下拉式選擇添加一個(gè)Item至定義索引位置
- 客戶端用JavaScript填充DropDownList控件 服務(wù)器端讀不到值
- asp.net省市三級(jí)聯(lián)動(dòng)的DropDownList+Ajax的三種框架(aspnet/Jquery/ExtJs)示例
- 利用js給DropdownList賦值實(shí)例
相關(guān)文章
JavaScript?Echarts柱狀圖label優(yōu)化中問(wèn)題針對(duì)講解
這篇文章主要介紹了JavaScript?Echarts柱狀圖label優(yōu)化中問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-12-12點(diǎn)擊進(jìn)行復(fù)制的JS代碼實(shí)例
這篇文章介紹了點(diǎn)擊進(jìn)行復(fù)制的JS代碼實(shí)例,有需要的朋友可以參考一下2013-08-08JavaScript實(shí)現(xiàn)京東快遞單號(hào)查詢
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)京東快遞單號(hào)查詢,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11JavaScript實(shí)現(xiàn)仿新浪微博大廳和騰訊微博首頁(yè)滾動(dòng)特效源碼
最近看到朋友用JavaScript實(shí)現(xiàn)仿新浪微博大廳和未登錄騰訊微博首頁(yè)滾動(dòng)效果,朋友使用jquery實(shí)現(xiàn)的,在網(wǎng)上看到有用js制作的也比較好,于是把我的內(nèi)容整理分享給大家,具體詳解請(qǐng)看本文2015-09-09使用JS實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能
這篇文章主要為大家詳細(xì)介紹了使用JS實(shí)現(xiàn)簡(jiǎn)單的圖片切換功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07iframe調(diào)用父頁(yè)面函數(shù)示例詳解
這篇文章主要介紹了iframe如何調(diào)用父頁(yè)面函數(shù),下面有個(gè)不錯(cuò)的示例,大家可以參考下2014-07-07ExtJs的Ext.Ajax.request實(shí)現(xiàn)waitMsg等待提示效果
這篇文章主要介紹了ExtJs的Ext.Ajax.request實(shí)現(xiàn)waitMsg等待提示效果,需要的朋友可以參考下2017-06-06javacript replace 正則取字符串中的值并替換【推薦】
replace() 方法用于在字符串中用一些字符替換另一些字符,或替換一個(gè)與正則表達(dá)式匹配的子串。這篇文章主要介紹了javacript replace 正則取字符串中的值并替換,需要的朋友可以參考下2018-09-09