.NET使用js制作百度搜索下拉提示效果(不是局部刷新)實(shí)現(xiàn)思路
大致思路:前臺(tái)放一個(gè)input標(biāo)簽,然后當(dāng)該標(biāo)簽內(nèi)的值輸入有變化的時(shí)候,調(diào)用后臺(tái)代碼查詢 符合條件的數(shù)據(jù)綁定ListBox。
具體實(shí)現(xiàn)思路:一個(gè)input,當(dāng)輸入值變化時(shí),調(diào)用后臺(tái)代碼。但是怎么調(diào)用呢,這個(gè)是個(gè)問(wèn)題了,在該input下放一個(gè)隱藏的服務(wù)器控件button,隱藏該控件,當(dāng)input里值變化時(shí),調(diào)用js,在js里觸發(fā)該按鈕的onclick事件,把具體的操作數(shù)據(jù)的代碼就可以放到onclick事件里了。但是這里的隱藏不是使用visable來(lái)隱藏的,而是使用:btnHelp.Style.Add("display", "none");[ps:btnHelp按鈕ID,放在Page_Load里],如果使用visable,則會(huì)造成在js里獲得不到該對(duì)象。 數(shù)據(jù)是有了,可是,怎樣使用上下鍵讓ListBox里的內(nèi)容顯示到Input上呢,很明顯,ListBox本身支持上下鍵的,只需要調(diào)用SelectedIndexChanged方法,然后為Input賦值即可。可是,怎么樣保證光標(biāo)就乖乖的聽(tīng)話,你按上下鍵它就自動(dòng)跳到ListBox里呢,好吧,寫(xiě)js吧,當(dāng)input里的值輸入完成,即:onkeyup事件里寫(xiě)即可。
具體代碼:
aspx代碼如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function abc() {
var inputV = document.getElementById("in").value;
//根據(jù)瀏覽器判斷
if (/msie/i.test(navigator.userAgent)) //ie瀏覽器
{
document.getElementById("lbltext").innerText = inputV;
}
else {//非ie瀏覽器,比如Firefox
document.getElementById("lbltext").innerHTML = inputV; //火狐等瀏覽器的賦值方式
}
}
function InputT() {
var f = document.getElementById("inpContent");
var abc = document.getElementById("btnHelp");
document.getElementById("btnHelp").click(); //觸發(fā)Button的onclick事件
}
//為input 添加的keydown事件
function InputKeyDownFocus() {
//方向鍵的ASCII值:上:38,下:40
if (event.keyCode == "38" || event.keyCode == "40") {
document.getElementById("lst").focus(); //使ListBox獲得焦點(diǎn)
}
else {
document.getElementById("inpContent").focus();
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
輸入內(nèi)容:
<br />
<input runat="server" id="inpContent" oninput="InputT()" onpropertychange="InputT()"
onkeyup="InputKeyDownFocus()" </br> />
<asp:ListBox runat="server" ID="lst" OnSelectedIndexChanged="lst_SelectedIndexChanged"
AutoPostBack="true"</asp:ListBox>
<asp:Button runat="server" ID="btnHelp" OnClick="btnHelp_Click" Text="隱藏按鈕" />
</div>
</form>
</body>
</html>
后臺(tái)cs代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
enum Direction
{
Up, Right, Down, Left
}
Direction dir;
protected void Page_Load(object sender, EventArgs e)
{
btnHelp.Style.Add("display", "none");
}
protected void lstShow_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lItem = (ListBox)sender;
string lItemValue = lItem.SelectedItem.Text;
txtInput.Text = lItemValue;
}
/// summary
/// 前臺(tái)調(diào)用的方法
/// /summary
/// param name="sender"/param
/// param name="e"/param
protected void btnHelp_Click(object sender, EventArgs e)
{
string inputStr = inpContent.Value.Trim(); //文本框輸入系統(tǒng)
Listobject listNew = new Listobject();
listNew.Add("abc");
listNew.Add("abcde");
listNew.Add("bcd");
listNew.Add("bcdef");
listNew.Add("bcdagb");
listNew.Add("bbccaa");
listNew.Add("aabbdd");
listNew.Add("ccaabbdd");
lst.Items.Clear(); //清除原有值
int i = 1;
foreach (object obj in listNew)
{
//符合條件的數(shù)據(jù)
if (obj.ToString().Contains(inputStr))
{
lst.Style.Add("display", "block");
lst.Items.Add(new ListItem(obj.ToString(), "" + i));
i++;
}
}
if (lst.Items.Count 0)
{
lst.SelectedIndex = 0;
}
inpContent.Focus();
}
/// summary
/// ListBox下拉框的值改變時(shí)
/// /summary
/// param name="sender"/param
/// param name="e"/param
protected void lst_SelectedIndexChanged(object sender, EventArgs e)
{
ListBox lItem = (ListBox)sender;
lst.Style.Add("display", "block");
string lItemValue = lItem.SelectedItem.Text;
inpContent.Value = lItemValue;
lst.Focus();
}
哦了
相關(guān)文章
ASP.NET2.0數(shù)據(jù)庫(kù)入門(mén)之SQL Server
ASP.NET2.0數(shù)據(jù)庫(kù)入門(mén)之SQL Server...2006-09-09asp.net中利用ajax獲取動(dòng)態(tài)創(chuàng)建表中文本框的值
通常在做主從表的數(shù)據(jù)錄入中,會(huì)碰到在一個(gè)頁(yè)面上同時(shí)錄入主表數(shù)據(jù)和從表數(shù)據(jù),主表的數(shù)據(jù)只有一條,從表的數(shù)據(jù)有一條到多條,這樣就要?jiǎng)討B(tài)創(chuàng)建從表數(shù)據(jù)錄入入口。2010-03-03巧妙使用JQuery Clone 添加多行數(shù)據(jù),并更新到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼
巧妙使用JQuery Clone 添加多行數(shù)據(jù),并更新到數(shù)據(jù)庫(kù)的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12asp.net 大文件上傳 之 改版了的SlickUpload.HttpUploadModule(Krystalware
以下代碼中所注釋的部分是所改版的地方。:) Krystalware.SlickUpload.dll2009-05-05Visual Studio 2017 針對(duì)移動(dòng)開(kāi)發(fā)的新特性匯總
Visual Studio是世界上最好的IDE之一,下面就讓我們一起來(lái)看看Visual Studio 2017中有哪些功能使得移動(dòng)開(kāi)發(fā)變得更加容易,感興趣的朋友通過(guò)本文學(xué)習(xí)下吧2017-05-05.NET?Core?使用委托實(shí)現(xiàn)動(dòng)態(tài)流程組裝的思路詳解
模擬管道模型中間件(Middleware)部分,運(yùn)用委托,進(jìn)行動(dòng)態(tài)流程組裝,本次代碼實(shí)現(xiàn)就直接我之前寫(xiě)的動(dòng)態(tài)代理實(shí)現(xiàn)AOP的基礎(chǔ)上改的,就不另起爐灶了,主要思路就是運(yùn)用委托,具體實(shí)現(xiàn)過(guò)程跟隨小編一起看看吧2022-01-01asp.net Repeater顯示父子表數(shù)據(jù),無(wú)閃爍
兩天在改項(xiàng)目bug,發(fā)現(xiàn)以前有人做的repeater顯示父子表結(jié)構(gòu)展開(kāi)和關(guān)閉子表數(shù)據(jù)時(shí)總是有閃爍,于是就試著改成無(wú)閃爍的,成功了,與大家分享.2009-12-12asp.NET中實(shí)現(xiàn)文件的壓縮和解壓(3種方式)
本篇文章主要介紹了asp.NET中實(shí)現(xiàn)文件的壓縮和解壓,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11