0

我有一个 .js 文件来使用 jQuery 控制搜索结果,我需要以某种方式再次加载的基本功能(即使 url 栏是 www.example.com/search#austria 并且用户在此链接上导航应该看到奥地利的结果) 这是:

function doSearch() {
    var el = $('#quickFilterInput');
    var val = el.val();
    var found = false;
    $('.ratesResults').hide();
    var w = '';
    db({
        'country_lower':{like:val.toLowerCase()}
    }).order('special','title').each( function(r) {
        found = true;
        var title = r.title;
        var special = false;
        if ( r.special == 'a' ) {
            special = true;
        }
        if ( title != val ) {
            title = title.replace(r.country, '');
        }
        w += '<div class="ratesBlock'+(special?' special':'')+'">';
        w += '<div class="ratesBlockItem ratesBlockTitle block-1">';
        w += '<p class="title-h4">'+title+'</p>';
        w += '</div>';
        w += '<div class="ratesBlockItem block-2">';
        w += '<p class="title-h4-notax">'+(r.price)+' &pound;</p>';
        w += '<p class="smaller">('+(number_format(r.price_including_tax, 3))+' &pound; incl. VAT)</p>';
        w += '</div>';
        w += '<div class="ratesBlockItem ratesBlockTitle block-3">';
        w += '<p class="title-h4">'+(r.plan_price)+'</p>';
        w += '</div>';
        w += '<div class="ratesBlockItem ratesBlockTitle block-4">';
        w += '<p class="title-h4">'+(r.plan_minutes)+'</p>';
        w += '</div>';
        w += '<div class="ratesBlockItem ratesBlockTitle block-5">';
        w += '<p class="title-h4">'+(r.plan_display)+'</p>';
        if ( r.plan_display != r.text_display ) {
            w += '<p class="smaller">'+(r.text_display)+'</p>';
        }
        w += '</div>';
        w += '<div class="buttonContainer noMobile">';
        w += '<a href="'+(r.href)+'" class="btn small secondaryCta" target="_top">';
        w += '<span class="noArrow">buy</span>';
        w += '</a>';
        w += '</div>';
        w += '</div>';
    });
    if ( found ) {
        $('.ratesTitle').html('Rates to '+val);
        $('#ratesSearchResults').html(w);
        $('.my_vcust').hide();
        $('.ratesResults').fadeIn();
        $('#ratesSearchResults .ratesBlock.special').each( function() {
            $(this).find('.buttonContainer').css('height', ($(this).height()-10)+'px');
        });
    }
}

我的意思是在 url 栏上添加哈希的例子,如果你重新加载页面,它会自动显示结果在 这里

我正在尝试在将加载 custom.js 的 html 页面上添加一个 javascript,以便它检查 url 中是否有国家/地区哈希(例如 www.example.com/search#austria 并在何时显示奥地利的结果用户通过该链接导航)

  if(window.location.hash) {
      var hash = window.location.hash.substring(1); 
      alert (hash);
// hash found in the url so let's try somehow to print the results?
doSearch();





  } else {
      // No hash found so let the page show the normal search bar
  }  

custom.js 更大,如果你需要我可以粘贴所有(实际上是 我的帖子末尾)

4

1 回答 1

0

在运行搜索之前尝试使用哈希值填充输入:

if(window.location.hash) {
    // hash found in the url
    var hash = window.location.hash.substring(1); 
    $('#quickFilterInput').val(hash);
    doSearch();
} else {
    // No hash found so let the page show the normal search bar
}  
于 2013-09-15T21:23:51.290 回答