简体   繁体   English

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

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

enter image description hereI am new to php and JavaScript/jQuery. 我在php和JavaScript / jQuery中是新手。 Before starting to Learn php I somehow know a little bit of htmL and css. 在开始学习php之前,我以某种方式知道htmL和CSS。

I have been editing a template I just downloaded and tumbled to this problem that I have been trying to figure out to look for a workaround 我一直在编辑我刚刚下载的模板,但遇到了这个问题,我一直在努力寻找解决方法

My problem is I can't make the result from my searchbox redirect to my update.php page on click of the result and clear the search when no match is found, and that is the output I am trying to accomplish. 我的问题是,单击结果时,我无法将搜索框的结果重定向到我的update.php页面,如果找不到匹配项,则清除搜索,这就是我要完成的输出。

I just can't make the same action from the anchor of my table when the user clicks the name do the same with my search bar. 当用户单击名称时,我只是无法通过表的锚点执行相同的操作,因此对我的搜索栏也是如此。

I hope someone can help me. 我希望有一个人可以帮助我。

(by the way, I am a magnifier and screen reader user due to some visual disability.) (顺便说一句,由于某些视觉障碍,我是放大镜和屏幕阅读器的用户。)

Thanks in advance :-) 提前致谢 :-)

<?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']);

this is how my index page Looks Like 这就是我的索引页面的样子

this is an exampLe of a search in action 这是行动搜寻的范例

i cLicked on a resuLt from the search 我选择了搜索结果

the resuLt is pLaced in the searchbox on cLick 结果在cLick的搜索框中显示

this is the update.php wherein the user shouLd be redirected upon cLicking the resuLt 这是update.php,其中在单击结果后应将用户重定向

i am sorry i wasn't abLe to add these Line of codes earLier. 对不起,我没办法添加这些代码行earLier。 i hope this couLd cLear things and make myseLf more understandabLe. 我希望这会有所帮助,并使自己更了解。

<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>

So you have an auto complete searchbox, and the results should not auto-complete the searchbox, but instead redirect to some other page? 因此,您有一个自动完成的搜索框,结果不应自动完成该搜索框,而应重定向到其他页面?

If so, then you have a problem, the default jquery auto complete won't help you here, so your function to add contents to result should be custom and then insert html to an object you provide styling to yourself. 如果是这样,则说明您有问题,默认的jquery自动完成功能对您无济于事,因此添加内容到结果中的函数应该是自定义的,然后将html插入到您提供样式的对象中。

something like this: 像这样的东西:

 $('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