繁体   English   中英

JavaScript / jQuery在脚本内添加锚标记

[英]JavaScript/jQuery adding anchor tag inside a script

我在php和JavaScript / jQuery中是新手。 在开始学习php之前,我以某种方式知道htmL和CSS。

我一直在编辑我刚刚下载的模板,但遇到了这个问题,我一直在努力寻找解决方法

我的问题是,单击结果时,我无法将搜索框的结果重定向到我的update.php页面,如果找不到匹配项,则清除搜索,这就是我要完成的输出。

当用户单击名称时,我只是无法通过表的锚点执行相同的操作,因此对我的搜索栏也是如此。

我希望有一个人可以帮助我。

(顺便说一句,由于某些视觉障碍,我是放大镜和屏幕阅读器的用户。)

提前致谢 :-)

<?php

$link = mysqli_connect("localhost", "root", "", "ajax_crud");

if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}

if(isset($_REQUEST['term'])){
$search_string = "SELECT * FROM name WHERE name LIKE ?";

if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "s", $param_term);

    $param_term = $_REQUEST['term'] . '%';

    if(mysqli_stmt_execute($stmt)){
        $result = mysqli_stmt_get_result($stmt);

        if(mysqli_num_rows($result) > 0){

            while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

echo "<p>" . $row["name"] . "</p>";
            }
        } else{

echo "<p>No matches found!</p>";    

        }
    } else{
        echo "ERROR: Could not able to execute $sql. " . 
mysqli_error($link);
    }
}

 mysqli_stmt_close($stmt);
}

mysqli_close($link);
?>

/* and the script

<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){

    var inputVal = $(this).val();
    var resultDropdown = $(this).siblings(".result");
    if(inputVal.length){
        $.get("backend-search.php", {term: inputVal}).done(function(data){

          resultDropdown.html(data);
        });
    } else{
        resultDropdown.empty();
    }enter image description here
});

$(document).on("click", ".result p", function(){
$(this).parents(".search- 
box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>


/* the part of the table where I can redirect the user to the update.php 
fiLe

echo sprintf('<td><a href="update.php?id=%s">%s</a></td>', $row['id'], 
$row['name']);

这就是我的索引页面的样子

这是行动搜寻的范例

我选择了搜索结果

结果在cLick的搜索框中显示

这是update.php,其中在单击结果后应将用户重定向

对不起,我没办法添加这些代码行earLier。 我希望这会有所帮助,并使自己更了解。

<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
    var inputVal = $(this).val();
    var resultDropdown = $(this).siblings(".result");
    if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
    resultDropdown.html(data);
        });
    } else{
        resultDropdown.empty();
    }
});

$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>

因此,您有一个自动完成的搜索框,结果不应自动完成该搜索框,而应重定向到其他页面?

如果是这样,则说明您有问题,默认的jquery自动完成功能对您无济于事,因此添加内容到结果中的函数应该是自定义的,然后将html插入到您提供样式的对象中。

像这样的东西:

 $('input[type="text"]').on("keyup input", function(){ var inputVal = $(this).val(); var resultDropdown = document.getElementById("result"); if(inputVal.length){ //$.get("backend-search.php", {term: inputVal}).done(function(data){ // exmaple data response var data = '<a href="https://stackoverflow.com" target="_blank" onclick="console.log(\\'option 1 got clicked\\'); return true">name1</a><br><a href="https://stackoverflow.com/questions/51924046/javascript-jquery-adding-anchor-tag-inside-a-script/51927190#51927190" target="_blank" onclick="console.log(\\'option 2 got clicked\\'); return true">name2</a>'; resultDropdown.innerHTML = data; } else{ resultDropdown.innerHTML = ""; } }); 
 <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <input type="text"> <div id="result"></div> </body> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM