gpt4 book ai didi

javascript - 单击搜索栏转到链接

转载 作者:行者123 更新时间:2023-11-29 09:36:41 25 4
gpt4 key购买 nike

我有一个搜索栏,可以建议数据库中的用户。目前,当单击用户名时,实际上什么也没有发生,用户名会转到搜索栏,但我如何做到这一点,以便单击用户名时,它会转到一个链接并在其后面添加用户名。

示例: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>

最佳答案

您可以通过更改 window.location.href 值导航到页面。所以,你可以这样做:

$(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();
});

关于javascript - 单击搜索栏转到链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57477398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com