news_ajax.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. (function($){
  2. $.fn.extend({
  3. select4:function(options){
  4. var defaults = {
  5. ajax_url:true
  6. }
  7. var options = $.extend(defaults, options);
  8. return this.each(function(){
  9. var mythis = $(this);
  10. var mythis2 = $('.y_scriptm');
  11. $(document).on("click",".select4_box li",function(){
  12. mythis.val($(this).find('a span.y_left').attr('title'));
  13. $(".select4_box").remove();
  14. });
  15. $(document).click(function(event) {
  16. $(".select4_box").remove();
  17. });
  18. $(".select4_box").click(function(event) {
  19. event.stopPropagation();
  20. });
  21. mythis.click(function(event) {
  22. var val = $(this).val();
  23. $.ajax({
  24. url:options.ajax_url,
  25. dataType:"json",
  26. data:{name:val},
  27. success:function(data){
  28. mythis2.html('');
  29. if(data.code == 200){
  30. var html = '<div class="select4_box"><ul>';
  31. var nametext ="";
  32. $.each(data.data,function(k,v){
  33. var nemeleng=v.subject;
  34. if(nemeleng != "undefined" && nemeleng != undefined){
  35. if (nemeleng.length>8) {
  36. nametext=nemeleng.substring(0,8)+"...";
  37. }else{
  38. nametext=nemeleng;
  39. };
  40. }
  41. html += '<li class="c"><a href="/news/details/'+v.id+'/">'+nametext+'</a></li>';
  42. });
  43. html+='</ul></div>'
  44. $(".select4_box").remove();
  45. mythis2.html(html);
  46. }
  47. }
  48. });
  49. });
  50. mythis.keyup(function(event) {
  51. if(event.keyCode==40){
  52. var index = $(".select4_box li.active").index()+1;
  53. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  54. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  55. }else if(event.keyCode==38){
  56. var index = $(".select4_box li.active").index()-1;
  57. if(index<0){
  58. index = $(".select4_box li").length-1;
  59. }
  60. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  61. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  62. }else if(event.keyCode==13){
  63. event.stopPropagation();
  64. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  65. return false;
  66. }else{
  67. mythis.trigger("click");
  68. }
  69. });
  70. });
  71. }
  72. });
  73. })(jQuery);
  74. $(function(){
  75. $(".y_headerscr").select4({"ajax_url":"/news/homeform"}); //需要填写接口
  76. $('.back_search_m .form a.submit').on('click',function(){
  77. var inptext=$('.y_headerscr').val();
  78. AjaxFn(inptext);
  79. })
  80. // 封装 ajax 方法
  81. function AjaxFn(txt){
  82. // console.log(txt)
  83. $.ajax({
  84. type: 'POST',
  85. url: '/news/homeform?name='+txt, //需要填写接口
  86. dataType: 'json',
  87. success: function(data){
  88. if(data.code == 200){
  89. $('.news_list .ns').html(ModuleWay(data.data)); //调用选择模块
  90. }else if(data.code == 300){
  91. $('.news_list .ns').html('<div class="wint"><img src="/image/icon-w.png" alt=""></div>');
  92. }
  93. },
  94. error: function(xhr, type){
  95. }
  96. });
  97. }
  98. function ModuleWay(conditions){
  99. var _html="";
  100. $.each(conditions.data,function (key,val) {
  101. _html +='<div class="plan_one">';
  102. _html +='<a href="/news/details/'+val.id+'/">';
  103. _html +='<div class="plan_one_title">';
  104. _html +='<p class="text">'+val.subject+'</p>';
  105. _html +=' <p> <span class="guis">'+val.source+' </span><i>·</i><span class="data">'+val.open_time+'</span></p>';
  106. _html +='</div>';
  107. _html +='<div class="plan_one_img"><img src="'+conditions.url+val.thumb[0]+'" alt=""></div>';
  108. _html +='</a>';
  109. _html +='</div>';
  110. })
  111. return _html ;
  112. }
  113. // $(document).keydown(function(event){
  114. // if(event.keyCode==13){
  115. // $(".y_lpsubmit").click();
  116. // }
  117. // });
  118. // 输入清除显示与隐藏
  119. $('.index_form input[type="search"]').on('keyup',function(){
  120. var txt = $(this).val();
  121. if(txt != ''){
  122. $('.index_form .w_cls').show();
  123. }else{
  124. $('.index_form .w_cls').hide();
  125. }
  126. })
  127. // 清除功能
  128. $('.index_form .w_cls').on('click',function(){
  129. $('.index_form input[type="search"]').val('');
  130. $(this).hide();
  131. })
  132. });
  133. // function search(){
  134. // var inptext=$('.y_headerscr').val();
  135. // window.location.href = '/house/news?name='+inptext;
  136. // }
  137. $('.m_lplist_inp').on('click',function(){
  138. $('.news_search_box').show();
  139. })
  140. $('.back-btn').on('click',function(){
  141. $('.news_search_box').hide();
  142. })