您的位置:首页 > 编程语言 > ASP

ASP.NET 根据TextBox输入的内容自动定位到ListBox中项

2012-08-03 14:24 519 查看
                这个效果很简单,直接上代码

<script type="text/javascript">

function OnTextChanged(textBoxID, listBoxID) {
var inputText = $.trim($("#" + textBoxID.toString()).val());
if (inputText.length != 0) {
for (var i = 0; i < document.getElementById(listBoxID.toString()).options.length; i++) {
document.getElementById(listBoxID).options[i].selected = false;
}

var sInputName = inputText.toUpperCase();
var iStopFlag = -1;
var iIndex = 0;
var sText;
while (iIndex < document.getElementById(listBoxID).options.length && iStopFlag == -1) {
sText = document.getElementById(listBoxID).options[iIndex].text.toUpperCase();
if (sText.indexOf(sInputName) != -1) {
document.getElementById(listBoxID).options[iIndex].selected = true;
//iStopFlag = 0;
}
iIndex++;
}
}
}

</script>


前台代码:

<table>
<tr>
<td>
<div style="margin-top: 0; text-align: right">
定位商户(请输入商户名称):
<asp:TextBox ID="txtLocateAllSeller" runat="server" onkeyup="OnTextChanged('txtLocateAllSeller','liboxAllSellers')"></asp:TextBox></div>
</td>
<td>
</td>
<td>
<div style="margin-top: 0; text-align: left">
定位商户(请输入商户名称):
<asp:TextBox ID="txtLocateSelectSeller" runat="server" onkeyup="OnTextChanged('txtLocateSelectSeller','selectSellers')"></asp:TextBox></div>
</td>
</tr>
</table>


这个js方法好处就是一个通用方法,只需要传入TextBox的ID和ListBox的ID即可。定位也相当于模糊查询。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息