Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 搜索栏上单击“转到链接”_Javascript_Html_Mysql - Fatal编程技术网

Javascript 搜索栏上单击“转到链接”

Javascript 搜索栏上单击“转到链接”,javascript,html,mysql,Javascript,Html,Mysql,我有一个搜索栏,可以从数据库中推荐用户。当前,当单击用户名时,实际上什么都没有发生,用户名会进入搜索栏,但我如何使其在单击用户名时进入链接并在其后面添加用户名 示例:www.test.com/user.php?u=用户名 <script type="text/javascript"> $(document).ready(function(){ $('.search-box input[type="text"]').on("keyup input", function(){

我有一个搜索栏,可以从数据库中推荐用户。当前,当单击用户名时,实际上什么都没有发生,用户名会进入搜索栏,但我如何使其在单击用户名时进入链接并在其后面添加用户名

示例: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();
});

数据能否为搜索返回多个值/用户名?如果是,那么将数据输出添加到您的帖子中。更好的是,如果我们确实需要更改页面,为什么不削减$this部分并直接重新定位页面?是的,我也考虑过这一点。由于我们正在远离当前页面,因此似乎没有必要设置输入值并清空结果。我之所以没有删除它,是因为代码已经出现在原始问题中,我没有足够的上下文来知道该代码对发布问题的人是否有意义。