Bez popisu

choice_city.js 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. var token = '';
  2. var qs = parseQueryString(window.location.href);
  3. var cityList = [];
  4. if (qs.token) {
  5. token = qs.token;
  6. }
  7. //自动唤起键盘
  8. $(".search input").trigger("click").focus()
  9. //返回上一页
  10. $("nav #back").click(function () {
  11. window.history.go(-1)
  12. })
  13. function deleteSearch () {
  14. $(".search input").val("")
  15. $(".container ul").html("")
  16. }
  17. /**
  18. * [getLoginInfo 获取城市登录信息]
  19. */
  20. function getLoginInfo() {
  21. var regionCode = $('.city-name').attr('data-code');
  22. $.ajax({
  23. type: "GET",
  24. url: BASEURL + '/city_list?type=fund&sort=true&token=' + token + '&time=' + Math.round(Date.now() / 1000),
  25. contentType: 'application/json;charset=utf-8',
  26. success: function(data) {
  27. cityList = data;
  28. return renderLoginInfo(cityList);
  29. },
  30. error: function(err) {
  31. return showMsg('获取城市信息失败:' + JSON.parse(err.responseText).error);
  32. }
  33. });
  34. }
  35. /**
  36. * [renderLoginInfo 渲染城市登录信息]
  37. * @param {[type]} cityInfo [城市登录信息]
  38. */
  39. function renderLoginInfo(cityInfo) {
  40. var bind_name = 'input';
  41. if (navigator.userAgent.indexOf("MSIE") != -1){
  42. bind_name = 'propertychange';
  43. }
  44. $(".search input").bind(bind_name, function(){
  45. var searchName = $(".search input").val().trim();
  46. var html = '';
  47. if(searchName != ""){
  48. $(".search em").css("display","block")
  49. for(key in cityInfo){
  50. if(key != "hot"){
  51. cityInfo[key].forEach(function (item,index) {
  52. if(item.region.indexOf(searchName) != -1){
  53. html += `<li data-code=${item.regionCode}>${item.region}</li>`
  54. }else if(item.pinyin.indexOf(searchName) != -1){
  55. html += `<li data-code=${item.regionCode}>${item.region}</li>`
  56. }
  57. })
  58. }
  59. }
  60. if(html != ""){
  61. $(".container ul").html(html)
  62. }else{
  63. html += `<li class="searchActive">无结果</li>`;
  64. $(".container ul").html(html)
  65. }
  66. }else{
  67. $(".search em").css("display","none")
  68. $(".container ul").html(html)
  69. }
  70. })
  71. $(".container ul").on("click", function(e) {
  72. var target = e.target;
  73. if ($(target).is("li")) {
  74. if($(target).attr("class") != "searchActive"){
  75. var city = $(target).text().trim();
  76. var code = $(target).attr('data-code');
  77. if (document.referrer.indexOf('&name=') === -1) {
  78. window.location.href = './fund_h5_api.html?token=' + token + '&name=' + city + '&code=' + code;
  79. } else {
  80. window.location.href = document.referrer.replace(/&name=[^&]+&code=[^&]+/, '&name=' + city + '&code=' + code);
  81. }
  82. }
  83. }
  84. });
  85. }
  86. /**
  87. * [showMsg 提示各种错误信息,3s后消失]
  88. */
  89. function showMsg(msg) {
  90. var msgBox = $('.alert-info');
  91. msgBox.children('p').text(msg);
  92. msgBox.show();
  93. setTimeout(function() {
  94. msgBox.hide();
  95. }, 3000);
  96. }
  97. $().ready(function () {
  98. getLoginInfo()
  99. })