[英]Search bar on click go to link
I have a search bar which suggests users from a database. 我有一个搜索栏,它可以建议数据库中的用户。 Currently when clicking on a username nothing really happens, the username goes to the searchbar, but how do i make it so that when clicking the username it goes to a link and adds the username behind it. 当前,当单击用户名时,实际上什么也没发生,用户名转到搜索栏,但是我该怎么做,以便在单击用户名时转到链接并在其后添加用户名。
example: www.test.com/user.php?u= ((the username)) 例如:www.test.com/user.php?u=((用户名))
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
You can navigate to a page by changing the window.location.href
value. 您可以通过更改window.location.href
值导航到页面。 So, you could do something like: 因此,您可以执行以下操作:
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
window.location.href = 'www.test.com/user.php?u=' + $(this).text();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.