search_ajax.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. var price = '待定';
  32. var unit = '';
  33. $.each(data.data, function (k, v) {
  34. if (v.sale_price != '' && v.sale_price != null && v.sale_price != 0) {
  35. price = v.sale_price;
  36. unit = v.price_unit;
  37. }
  38. var nemeleng = v.name;
  39. if (nemeleng.length > 8) {
  40. var nametext = nemeleng.substring(0, 8) + "...";
  41. } else {
  42. var nametext = nemeleng;
  43. }
  44. ;
  45. 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><span class="y_right"><em>' + price + '</em>' + unit + '</span></a></li>';
  46. });
  47. html += '</ul></div>'
  48. $(".select4_box").remove();
  49. mythis2.html(html);
  50. }
  51. }
  52. });
  53. });
  54. mythis.keyup(function (event) {
  55. if (event.keyCode == 40) {
  56. var index = $(".select4_box li.active").index() + 1;
  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 == 38) {
  60. var index = $(".select4_box li.active").index() - 1;
  61. if (index < 0) {
  62. index = $(".select4_box li").length - 1;
  63. }
  64. $(".select4_box li").eq(index).addClass('active').siblings().removeClass('active');
  65. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  66. } else if (event.keyCode == 13) {
  67. event.stopPropagation();
  68. mythis.val($(".select4_box li.active span.y_left").attr('title'));
  69. return false;
  70. } else {
  71. mythis.trigger("click");
  72. }
  73. });
  74. });
  75. }
  76. });
  77. })(jQuery);
  78. $(function () {
  79. $(".y_headerscr").select4({"ajax_url": "/json/searchname"});
  80. $('.back_search_m .form a').on('click', function () {
  81. var inptext = $('.y_headerscr').val();
  82. window.location.href = '/house/search?name=' + inptext;
  83. })
  84. $(document).keydown(function (event) {
  85. if (event.keyCode == 13) {
  86. $(".y_lpsubmit").click();
  87. }
  88. });
  89. });
  90. function search() {
  91. var inptext = $('.y_headerscr').val();
  92. window.location.href = '/house/search?name=' + inptext;
  93. }