search_ajax.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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:{title:val},
  27. success:function(data){
  28. mythis2.html('');
  29. if(data.code == 200){
  30. var html = '<div class="select4_box"><ul>';
  31. $.each(data.data,function(k,v){
  32. var nemeleng=v.name;
  33. if (nemeleng.length>20) {
  34. var nametext=nemeleng.substring(0,18)+"...";
  35. }else{
  36. var nametext=nemeleng;
  37. };
  38. html += '<li class="c"><a href="/house/'+v.id+'/"><span class="y_left" title="'+v.name+'"><em>'+nametext+'</em><i>['+v.city_name+']</i></span></a></li>';
  39. });
  40. html+='</ul></div>'
  41. $(".select4_box").remove();
  42. mythis2.html(html);
  43. }
  44. }
  45. });
  46. });
  47. mythis.keyup(function(event) {
  48. if(event.keyCode==40){
  49. var index = $(".select4_box li.active").index()+1;
  50. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  51. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  52. }else if(event.keyCode==38){
  53. var index = $(".select4_box li.active").index()-1;
  54. if(index<0){
  55. index = $(".select4_box li").length-1;
  56. }
  57. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  58. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  59. }else if(event.keyCode==13){
  60. event.stopPropagation();
  61. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  62. return false;
  63. }else{
  64. mythis.trigger("click");
  65. }
  66. });
  67. });
  68. }
  69. });
  70. })(jQuery);
  71. $(function(){
  72. $(".search_input").select4({"ajax_url":"/house/searchname"});
  73. $('a#search_butt').on('click',function(){
  74. search();
  75. })
  76. $(document).keydown(function(event){
  77. if(event.keyCode==13){
  78. search();
  79. }
  80. });
  81. });
  82. function search(){
  83. var inptext=$('.search_input').val();
  84. window.location.href = '/house/search?name='+inptext;
  85. }