我用jQuery製作搜索插件。
當用戶在搜索項目後按下搜索輸入中的回車鍵時,我需要將頁面重定向到所選項目href
。用jQuery搜索項目並按下輸入頁面將重定向到選定的項目鏈接
jQuery.expr[':'].contains = function (a, i, m) {
return jQuery(a).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};
jQuery(function ($) {
$('input[name="search"]').keyup(function() {
var searchterm = $(this).val();
if (searchterm.length > 1) {
var match = $('.item_name:contains("' + searchterm + '")');
var nomatch = $('.item_name:not(:contains("' + searchterm + '"))');
match.addClass('selected');
nomatch.addClass('uk-hidden');
} else {
$('.item_name').removeClass('uk-hidden');
$('.item_name').removeClass('selected');
}
});
var statematch = $('.item_name:contains("استان")');
statematch.remove().parent();
});
.uk-hidden{
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="uk-search">
<input class="uk-search-field" type="search" name="search" placeholder="search here">
</div>
<div class="ad-city-list">
<div class="item_name"><a href="tehran.html">Tehran</a></div>
<div class="item_name"><a href="istanbul.html">Istanbul</a></div>
<div class="item_name"><a href="tabriz.html">Tabriz</a></div>
</div>
我怎麼能這樣做?
TNX我的朋友用自己的方式爲工作更正確: - * –