您的位置:首页 > Web前端 > JavaScript

使用js实现的邮箱自动补全

2018-03-27 15:27 471 查看
1.页面代码
<input type="text" id="username" name="username" list="input_list"/><datalist id="input_list"></datalist>
<script src="/static/test.js" type="application/javascript"></script>
2.JS代码
tsc = {};
tsc.result = {
//初始化页面
init: function () {
//初始化绑定事件
tsc.result.event();
},
//给标签绑定事件
event : function(){
tsc.result.inputlist($('#username'),$('#input_list'));
},
//邮箱账号补全的方法
inputlist : function (input,list) {
var mailBox = [
"@163.com",
"@126.com",
"@qq.com",
"@sina.com",
"@yahoo.com"
];
input.bind('input propertychange',function(){
var key = input.val();
if(key.indexOf('@') != -1){
key = key.slice(0,key.indexOf('@'));
var mailBoxLen = mailBox.length;
var html = "";
for(var i = 0; i < mailBoxLen; i++){
html += '<option value="'+ key + mailBox[i] + '"></option>';
}
list.html(html);
}else{
list.html('');
}
})
}
};

$(function(){
tsc.result.init();
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: