从DropDownList到TextBox,也就是当DropDownList选择时,把DropDownList的TEXT或VALUE值赋给TEXTBOX
//ddl是dropdownlist控件的名称,txt是textbox控件的名称
//把dropdownlist的Text值赋给TEXTBOX
function DdlSelectedTextToTxt(ddl,txt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
document.all(txt).value=document.all(ddl).options[document.all(ddl).selectedIndex].text;
return false;
}
//把dropdownlist的VALUE值赋给TEXTBOX
function DdlSelectedTextToTxt(ddl,txt)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
document.all(txt).value=document.all(ddl).options[document.all(ddl).selectedIndex].value;
return false;
}
把方法绑定到 DropDownList的onchange事件上
ddl.Attributes.Add("onchange", "return DdlSelectedTextToTxt('" + ddl.ClientID + "','" + txt.ClientID + "')");
从TextBox到DropDownList,也就是当textbox的值改变时,从DropDownList的text或value中查找最相近的值(下面的代码只是在DropDownList的text中查找),并把DropDownList的TEXT或VALUE值赋给TEXTBOX。
1.先比较textbox与dropdownlist的值
//返回值:bool, ddl所有项与txt.text值进行匹配,若找到返回true,否则返回false
function Match_DdlToTxt(txt,ddl)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
var returnValue=false;
var txtValue=document.all(txt).value;
for( i=0;i<document.all(ddl).options.length;i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(IsMatch(txtValue,document.all(ddl).options[i].text)==true)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
document.all(txt).value=document.all(ddl).options[i].text;
document.all(ddl).options[i].selected=true;
returnValue= true;
break;
}
}
return returnValue;
}
//返回值:bool,匹配sValue1与sValue2项,sValue2是否包含sValue1,包含返回true,否则返回false
function IsMatch(sValue1,sValue2)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
var index=sValue2.indexOf(sValue1);
if(index>=0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return true;
}
return false;
}
2.设置当textbox中输入是回车键时响应比较查询事件
function ChkKeyEnterDdl(txt,ddl)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
if(event.keyCode==13)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if(Match_DdlToTxt(txt,ddl)==false)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
alert("数据不匹配!");
}
return false;
}
}
3.把方法绑定到textbox的onkeypress事件或onkeydown事件上
TextBox1.Attributes.Add("onkeydown", "return ChkKeyEnterDdl('" + txt.ClientID + "','" + ddl.ClientID + "')");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
或
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
TextBox1.Attributes.Add("onkeypress", "return ChkKeyEnterDdl('" + txt.ClientID + "','" + ddl.ClientID + "')");