【问题标题】:JavaScript/jQuery adding anchor tag inside a scriptJavaScript/jQuery 在脚本中添加锚标记
【发布时间】:2018-08-20 04:10:13
【问题描述】:

在此处输入图像描述我是 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']);

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

this is the update.php wherein the user shouLd be redirected upon cLicking the resuLt

很抱歉,我无法更早地添加这些代码行。 我希望这可以澄清事情并使我自己更容易理解。

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

【问题讨论】:

  • 你的问题对我来说不是很清楚。我试图从您给出的内容中了解您的 HTML 的形状,并认为您有一个要搜索的文本字段,当用户键入内容时,您将从 PHP 脚本中获取结果并填充 .result 下拉列表。你能添加.result所在页面的HTML和backend-search.php的输出(它应该是&lt;option&gt;标签的集合)。另外,我不确定您的意思是 result from my searchbox redirect to my update.php page 请提供更多信息(更多代码、屏幕截图等)
  • 我想要实现的是将用户重定向到 update.php 页面,并从搜索框中获得相应的结果,就像图片上的一样。我评论了可以重定向用户的代码通过单击 tabLe 中的名称来更新.php 页面(即 echo sprintf('%s', $ row['id'], $row['name']); ) 我的问题是当我单击搜索结果时,我无法弄清楚如何在我的 jquery 脚本中放置相同的锚标记,而不是结果只会出现在搜索框中..(我正在尝试添加照片)
  • 我刚刚添加了关于我的问题的照片..

标签: javascript php jquery


【解决方案1】:

所以你有一个自动完成的搜索框,结果不应该自动完成搜索框,而是重定向到其他页面?

如果是这样,那么您就有问题了,默认的 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>

【讨论】:

  • 不,这不是我的意思,不是 autocompLete,而是通过点击搜索框的结果提要转移到页面 update.php.. 我想我必须替换这部分:$(document ).on("click", ".result p", function(){ $(this).parents(".search-box").find('input[type="text"]').val($ (this).text()); $(this).parent(".result").empty();
  • 这不正是我在代码 sn-p 中所做的吗?单击自定义自动完成现在是一个简单的锚点。由于需要重定向,因此将值设置为搜索框不再有用,但正如您所见,每个锚点都有自己的 onclick,因此您可以做任何您想做的事情。
  • 我刚刚添加了我一直在使用的脚本.. 请查看更新后的帖子..
  • 没有乔纳森,不是我一直在寻找的答案.. 无论如何谢谢你的答案..
  • 顺便说一句,我正在使用 ajax 为每个按键输入提供每个可能的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 2015-08-14
  • 1970-01-01
  • 2013-03-02
相关资源
最近更新 更多