(function($){
$.fn.extend({
select4:function(options){
var defaults = {
ajax_url:true
}
var options = $.extend(defaults, options);
return this.each(function(){
var mythis = $(this);
var mythis2 = $('.y_scriptm');
$(document).on("click",".select4_box li",function(){
mythis.val($(this).find('a span.y_left').attr('title'));
$(".select4_box").remove();
});
$(document).click(function(event) {
$(".select4_box").remove();
});
$(".select4_box").click(function(event) {
event.stopPropagation();
});
mythis.click(function(event) {
var val = $(this).val();
$.ajax({
url:options.ajax_url,
dataType:"json",
data:{name:val},
success:function(data){
mythis2.html('');
if(data.code == 200){
var html = '
';
var nametext ="";
$.each(data.data,function(k,v){
var nemeleng=v.subject;
if(nemeleng != "undefined" && nemeleng != undefined){
if (nemeleng.length>8) {
nametext=nemeleng.substring(0,8)+"...";
}else{
nametext=nemeleng;
};
}
html += '- '+nametext+'
';
});
html+='
'
$(".select4_box").remove();
mythis2.html(html);
}
}
});
});
mythis.keyup(function(event) {
if(event.keyCode==40){
var index = $(".select4_box li.active").index()+1;
$(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
mythis.val($(".select4_box li.active span.y_left").attr('title'));
}else if(event.keyCode==38){
var index = $(".select4_box li.active").index()-1;
if(index<0){
index = $(".select4_box li").length-1;
}
$(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
mythis.val($(".select4_box li.active span.y_left").attr('title'));
}else if(event.keyCode==13){
event.stopPropagation();
mythis.val($(".select4_box li.active span.y_left").attr('title'));
return false;
}else{
mythis.trigger("click");
}
});
});
}
});
})(jQuery);
$(function(){
$(".y_headerscr").select4({"ajax_url":"/news/homeform"}); //需要填写接口
$('.back_search_m .form a.submit').on('click',function(){
var inptext=$('.y_headerscr').val();
AjaxFn(inptext);
})
// 封装 ajax 方法
function AjaxFn(txt){
// console.log(txt)
$.ajax({
type: 'POST',
url: '/news/homeform?name='+txt, //需要填写接口
dataType: 'json',
success: function(data){
if(data.code == 200){
$('.news_list .ns').html(ModuleWay(data.data)); //调用选择模块
}else if(data.code == 300){
$('.news_list .ns').html('');
}
},
error: function(xhr, type){
}
});
}
function ModuleWay(conditions){
var _html="";
$.each(conditions.data,function (key,val) {
_html +='';
})
return _html ;
}
// $(document).keydown(function(event){
// if(event.keyCode==13){
// $(".y_lpsubmit").click();
// }
// });
// 输入清除显示与隐藏
$('.index_form input[type="search"]').on('keyup',function(){
var txt = $(this).val();
if(txt != ''){
$('.index_form .w_cls').show();
}else{
$('.index_form .w_cls').hide();
}
})
// 清除功能
$('.index_form .w_cls').on('click',function(){
$('.index_form input[type="search"]').val('');
$(this).hide();
})
});
// function search(){
// var inptext=$('.y_headerscr').val();
// window.location.href = '/house/news?name='+inptext;
// }
$('.m_lplist_inp').on('click',function(){
$('.news_search_box').show();
})
$('.back-btn').on('click',function(){
$('.news_search_box').hide();
})