• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

ajax实现输入框文字改变展示下拉列表的效果示例

jquery 搞代码 4年前 (2021-12-27) 34次浏览 已收录 0个评论

这篇文章主要介绍了通过ajax实现输入框文字改变展示下拉列表的效果,需要的朋友可以参考下

1.样式

代码如下:

<!–
body{background:#fff}
.Menu {
position:relative;
width:180px;
height:120px;
z-index:1;
background: #EEE;
border:1px solid #666;
margin-top:-100px;
display:none;
}
.Menu2 {
position: absolute;
left:0;
top:0;
width:100%;
height:120px;
overflow:hidden;
z-index:1;
OVERFLOW-y:auto;
}
.Menu2 ul{margin:0;padding:0}
.Menu2 ul li{width:100%;height:25px;line-height:20px;text-indent:15px;
border-bottom:1px dashed #999;color:#333;cursor:pointer;
change:expression(
this.onmouseover=function(){
this.style.background=””;
},
this.onmouseout=function(){
this.style.background=””;
}
)
}
input{width:120px}
#List1,#List2{left:0px;top:103px;}
–>

2. html脚本

代码如下:
……..省略常规脚本

汽车品牌名:

<input type="text" id="generalBrandName" name="generalBrandName" value="${*.generalBrandName}" style="width:180px" data-validation-engine="validate[required]" disabled=”disabled” onfocus=”showAndHide(‘List1′,’show’);” onblur=”showAndHide(‘List1′,’hide’);”/>

必填*

……..省略常规脚本

汽车厂商名:

<input type="text" id="brandName" name="brandName" value="${*.brandName}" style="width:180px" data-validation-engine="validate[required]" disabled=”disabled” onfocus=”showAndHide(‘List2′,’show’);” onblur=”showAndHide(‘List2′,’hide’);” />

必填*

3.通过JS来实现ajax异步请求 根据输入的内容过滤

代码如下:
//页面加载的时候

jQuery(function($) {
if (navigator.userAgent.indexOf(“MSIE”) > 0) {
document.getElementById(‘generalBrandName’).attachEvent(“onPropertyChange”, appendList);
document.getElementById(‘brandName’).attachEvent(“onPropertyChange”, appendList);
}
else if (navigator.userAgent.indexOf(“Firefox”) > 0) {
document.getElementById(‘generalBrandName’).addEventListener(“input”, appendList, false);
document.getElementById(‘brandName’).addEventListener(“input”, appendList, false);
}
});

//////// 预加载
jQuery(function($) {
txtValue = $(“#generalBrandName”).val();
//////// 给txtbox绑定键盘事件
$(“#generalBrandName”).bind(“keyup”, function() {
var curre来源gao.dai.ma.com搞@代*码网ntValue = $(this).val();
if (currentValue != txtValue) {
appendList(‘List1’,currentValue);
txtValue = currentValue;
}
});

txtValue = $(“#brandName”).val();
//////// 给txtbox绑定键盘事件
$(“#brandName”).bind(“keyup”, function() {
var currentValue = $(this).val();
if (currentValue != txtValue) {
appendList(‘List2’,currentValue);
txtValue = currentValue;
}
});

});

//实现动态显示下拉列表内容的function

//根据输入框中的值来筛选obj中的值
function appendList(obj,value){
value = encodeURIComponent(value); value = encodeURIComponent(value); //两次使用encodeURI()
switch(obj){
case “List1”: //根据车品牌名来刷选List1中的值
$.getJSON(
ctx + “/car/carmodel/**.do”,
{keyWord : value, id : new Date().getTime()},
function (json) {
createLis(‘ListLi1’,json);
}
);
break;
case “List2”: //根据车厂商名来刷选List2中的值
$.getJSON(
ctx + “/car/carmodel/**.do”,
{keyWord : value, id : new Date().getTime()},
function (json) {
createLis(‘ListLi2’,json);
}
);
break;
}
}

function createLis(obj,json){
switch(obj){
case “ListLi1”: //根据车品牌名来刷选List1中的值
var executerDiv = document.getElementById(obj); //动态生成下拉列表框
executerDiv.innerHTML=””;
var ul=document.createElement(“ul”);
$.each(json, function (i, item) {
var li=document.createElement(“li”);
var str = “getValue(‘generalBrandName’,'”+item.brandNameGeneral+”‘,’brandIdGeneral’,'”+item.brandIdGeneral+”‘);showAndHide(‘List1′,’hide’)”;
li.setAttribute(“onmousedown”,str);
li.appendChild(document.createTextNode(item.brandNameGeneral));
ul.appendChild(li);
});
executerDiv.appendChild(ul);
break;
case “ListLi2”: //根据车厂商名来刷选List2中的值
var executerDiv = document.getElementById(obj); //动态生成下拉列表框
executerDiv.innerHTML=””;
var ul=document.createElement(“ul”);
$.each(json, function (i, item) {
var li=document.createElement(“li”);
var str = “getValue(‘brandName’,'”+item.brandName+”‘,’brandId’,'”+item.brandId+”‘);showAndHide(‘List1′,’hide’)”;
li.setAttribute(“onmousedown”,str);
li.appendChild(document.createTextNode(item.brandName));
ul.appendChild(li);
});
executerDiv.appendChild(ul);
break;
}
}
//显示或者隐藏层
function showAndHide(obj,types){
var Layer=window.document.getElementById(obj);
switch(types){
case “show”:
Layer.style.display=”block”;
appendList(obj,”);
break;
case “hide”:
Layer.style.display=”none”;
break;
}
}

//获取选中节点的内容
function getValue(obj1,str,obj2,idx){
var input=window.document.getElementById(obj1);
input.value=str;
var input=window.document.getElementById(obj2);
input.value=idx;
}

4.展示效果

以上就是ajax实现输入框文字改变展示下拉列表的效果示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:ajax实现输入框文字改变展示下拉列表的效果示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址