JavaScript与DropDownList 区别分析(2)


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选择到指定项
选择dropdownlist中值为“我得选择”得项

复制代码 代码如下:


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选项的选中

复制代码 代码如下:


<asp:ListBox 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>取消绑定</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 为选中的值。如果在模态窗口中使用,可以用如下代码:
window.returnValue=select1Text;//这是返回给父窗体的值
window.close();


javascript中设定dropdownlist哪一项为当前选中项
方法1:
i = 2
document.all.dropdownlistID.options[i].selected=true
方法2:
obj.selectedIndex = 2;
方法3:
obj.value="你要设的数值。"//Dropdownlist就会自动把那个值设为当前。
javascript清除dropdownlist的项

复制代码 代码如下:


//清除原有项
function clearitem(){
var drp1 = document.getElementById("drp1");
while(drp1.options.length>0)
{
drp1.options.remove(0);
}
}


动态更改方法(根据城市代码取得该市商业区并添加到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(); //清除原有下拉项
var str = htp.responseText;
var opt = str.split(',');
var s = opt.length
for(var j = 0;j<s;j++)
{
var newOption = document.createElement("OPTION");   //定义一个新的项
var ff = opt[j].split('|');
   newOption.text = ff[1];
   newOption.value = ff[1];
   drp1.options.add(newOption);
  }
}
}
htp.send()
}


JavaScript实现DropDownList(Select)三级联动无刷新

复制代码 代码如下:


<script language=javascript>
function CountryChange(){
countryid=document.getElementById("ddlContry").value;
if(countryid==null||countryid==""){
alert("请选择所属国家");
CountryDel("ddlProvince");//清空DropDownList
CountryDel("ddlCity");//清空DropDownList
return false;
}
var countrystring="";
var posturl=location.protocol+"//"+location.hostname+"//soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid;
countrystring=openUrl(posturl);
if(countrystring=="-2"){//查询失败
alert("数据查询失败");
return false;
}
//分割并写入DropDownList
CountryDel("ddlProvince");//清空DropDownList
CountryDel("ddlCity");//清空DropDownList
if(countrystring==""){
return false;
}
var stringarray=countrystring.split("|");
for(var i=0;i<stringarray.length;i++){//重写DropDownList
//拆分内部数组
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("请选择所属省");
CountryDel("ddlCity");//清空DropDownList
return false;
}
var countrystring="";
var posturl=location.protocol+"//"+location.hostname+"//soleweb//AjaxEnjine//AreaShow.aspx?AreaId="+countryid;
countrystring=openUrl(posturl);
if(countrystring=="-2"){//查询失败
alert("数据查询失败");
return false;
}
//分割并写入DropDownList
CountryDel("ddlCity");//清空DropDownList
if(countrystring==""){
return false;
}
var stringarray=countrystring.split("|");
for(var i=0;i<stringarray.length;i++){//重写DropDownList
//拆分内部数组
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代码

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdpgsw.html