search_ajax.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 search_hide = $('.js_searchHone_list');
  11. $(document).on("click", ".select4_box li", function () {
  12. mythis.val($(this).find('a span').attr('title'));
  13. $(".select4_box").remove();
  14. search_hide.hide();
  15. });
  16. mythis.click(function (event) {
  17. $('.js_hotHomee_list').show();
  18. var val = $(this).val();
  19. $.ajax({
  20. url: options.ajax_url,
  21. dataType: "json",
  22. data: {title: val},
  23. success: function (data) {
  24. // console.log(data);
  25. search_hide.html('');
  26. if (data.code == 200) {
  27. $('.js_hotHomee_list').hide();
  28. search_hide.show();
  29. var html = '<div class="select4_box"><ul>';
  30. var price = '';
  31. var unit = '';
  32. $.each(data.data, function (k, v) {
  33. if (v.sale_price == 0 || v.sale_price == null || v.sale_price == '') {
  34. var price = '待定';
  35. var unit = '';
  36. } else {
  37. price = v.sale_price;
  38. unit = v.price_unit;
  39. }
  40. var vname = v.name;
  41. if (vname.length > 10) {
  42. nametext = vname.substring(0, 8) + "...";
  43. } else {
  44. nametext = vname;
  45. };
  46. html += '<li>';
  47. html += '<a href="/house/' + v.id + '">';
  48. html += '<p class="left_name">';
  49. html += '<span title="' + v.name + '">' + v.name + '</span><em>[' + v.city_name + ']</em>';
  50. html += '</p>';
  51. html += '<i>' + price + unit + '</i>';
  52. html += '</a>';
  53. html += '</li>';
  54. });
  55. html += '</ul></div>'
  56. $(".select4_box").remove();
  57. search_hide.html(html);
  58. }
  59. }
  60. });
  61. });
  62. mythis.keyup(function (event) {
  63. if (event.keyCode == 40) {
  64. var index = $(".select4_box li.active").index() + 1;
  65. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  66. mythis.val($(".select4_box li.active span").attr('title'));
  67. } else if (event.keyCode == 38) {
  68. var index = $(".select4_box li.active").index() - 1;
  69. if (index < 0) {
  70. index = $(".select4_box li").length - 1;
  71. }
  72. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  73. mythis.val($(".select4_box li.active span").attr('title'));
  74. } else if (event.keyCode == 13) {
  75. event.stopPropagation();
  76. mythis.val($(".select4_box li.active span").attr('title'));
  77. return false;
  78. } else {
  79. mythis.trigger("click");
  80. }
  81. });
  82. });
  83. }
  84. });
  85. })(jQuery);
  86. $(function () {
  87. $(".js_puicSearch_text").select4({"ajax_url": "/json/searchresult"});
  88. $('.js_js_puicSearch_buttom').on('click', function () {
  89. var inptext = $('.js_puicSearch_text').val();
  90. window.location.href = '/house/search?name=' + inptext;
  91. })
  92. $(document).keydown(function (event) {
  93. if (event.keyCode == 13) {
  94. $(".js_js_puicSearch_buttom").click();
  95. }
  96. });
  97. $(document).click(function() {
  98. $('.js_hotHomee_list,.js_searchHone_list').hide();
  99. })
  100. $('.js_yv2_bread_search').on('click',function(e){
  101. e?e.stopPropagation():event.cancelBubble = true;
  102. })
  103. });