Jquery Auto建议点击标签

时间:2014-12-19 19:27:22

标签: javascript jquery json autocomplete

我在网站http://www.4songs.pk/上使用搜索框的jquery自动建议。自动建议工作正常。我想建议必须点击自定义A标签。我想要的建议必须是点击链接。

HTML CODE

<form role="form" id="searchform" method="get" action="http://www.4songs.pk/search">
    <div class="row">
      <div class="col-xs-12 col-sm-6 col-md-8">
        <div class="input-group" style="padding:10px 0;">
          <input type="text" autocomplete="off" class="form-control" id="query" name="q" placeholder="Type Track Name and Press Search Button" required>
            <span class="input-group-btn">
                <button class="btn btn-success" id="searchsubmit" type="submit"><span class="glyphicon glyphicon-search"></span></button>
            </span>
        </div><!-- /input-group -->
      </div><!-- /.col-lg-6 -->
    </div><!-- /.row -->
</form>

JS代码

function doSomething (txt) {
    $('#selection').text ('You selected: ' + txt);
}

$('#query').autocomplete({
    source: function (request, response) {
        var url = 'http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=' + escape (request.term) + '&format=5&alt=json&callback=?';
        $.getJSON (url, function (json) {
            var results = [];
            $.each (json[1], function (ix, val) {
                results.push ('<a href="">'+val [0]+'</a>');
            });
            response (results);
        });
    },
    select: function(e, ui) {
        if (e.which === 13 ) {
            return false;
        }
    },
    delay:200
});
$('#query').keypress (function (ev) {
    if (ev.keyCode === 13) {
        $('#query').autocomplete ('close');
        $('#searchform').submit();
        var txt = $(this).val ();
        doSomething (txt);
        return (false);
    }
});

小提琴链接http://jsfiddle.net/nmz95e7d/

1 个答案:

答案 0 :(得分:2)

更新您的select语句以重定向网址

 select: function(e, ui) {   
            location.href="whereyouwanttogo.com/" + ui.item.label;
        },

完整代码

            <form role="form" id="searchform" method="get" action="http://www.4songs.pk/search">
            <div class="row">
              <div class="col-xs-12 col-sm-6 col-md-8">
                <div class="input-group" style="padding:10px 0;">
                  <input type="text" autocomplete="off" class="form-control" id="query" name="q" placeholder="Type Track Name and Press Search Button" required>                       
                </div><!-- /input-group -->
                <div id="tag_update"></div>
              </div><!-- /.col-lg-6 -->
            </div><!-- /.row -->
        </form>

JS

function doSomething (txt) {
    $('#selection').text ('You selected: ' + txt);
}

$('#query').autocomplete({
    source: function (request, response) {
        var url = 'http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=' + escape (request.term) + '&format=5&alt=json&callback=?';
        $.getJSON (url, function (json) {
            var results = [];
            $.each (json[1], function (ix, val) {
                results.push (val [0]);
            });
            response (results);
        });
    },
    select: function(e, ui) {
        //location.href="http://www.4songs.pk/search?q=" + ui;
        window.open('http://www.4songs.pk/search?q='+ ui.item.label.replace(/\s/g, "+"),'_blank');
        if (e.which === 13 ) {
            return false;
        }
    },
    delay:200
});