【问题标题】:How to change on click action to go to link instead of inputting text如何更改点击操作以转到链接而不是输入文本
【发布时间】:2019-12-15 21:50:21
【问题描述】:

所以,我正在使用AJAX Live Search PDO 我更改了后端搜索以链接显示的单词。问题是如果您单击表格,它不会将它们带到链接,而只是输入文本。我希望能够链接整行......所以如果他们不只是点击搜索的单词。

我非常彻底地阅读了document,但没有找到答案。

我认为它要么在 search-form.php 第 62 行的 JavaScript 上,要么在 backend-search.php 之一上

我加了

<a href=\"" . $row["link"] . "\"> to the backend-search file.

search-form.php

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database Search</title>
<style type="text/css">
    body{
        font-family: Arail, sans-serif;
    }
    /* Formatting search box */
    .search-box{
        width: 300px;
        position: relative;
        display: inline-block;
        font-size: 14px;
    }
    .search-box input[type="text"]{
        height: 32px;
        padding: 5px 10px;
        border: 1px solid #CCCCCC;
        font-size: 14px;
    }
    .result{
        position: absolute;        
        z-index: 999;
        top: 100%;
        left: 0;
    }
    .search-box input[type="text"], .result{
        width: 100%;
        box-sizing: border-box;
    }
    /* Formatting result items */
    .result p{
        margin: 0;
        padding: 7px 10px;
        border: 1px solid #CCCCCC;
        border-top: none;
        cursor: pointer;
    }
    .result p:hover{
        background: #f2f2f2;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<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>
</head>
<body>
    <div class="search-box">
        <input type="text" autocomplete="off" placeholder="Search country..." />
        <div class="result"></div>
    </div>
</body>
</html>

后端搜索.php

<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
try{
    $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", "");
    // Set the PDO error mode to exception
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e){
    die("ERROR: Could not connect. " . $e->getMessage());
}

// Attempt search query execution
try{
    if(isset($_REQUEST["term"])){
        // create prepared statement
        $sql = "SELECT * FROM countries WHERE name LIKE :term";
        $stmt = $pdo->prepare($sql);
        $term = $_REQUEST["term"] . '%';
        // bind parameters to statement
        $stmt->bindParam(":term", $term);
        // execute the prepared statement
        $stmt->execute();
        if($stmt->rowCount() > 0){
            while($row = $stmt->fetch()){
                echo "<p><a href=\"" . $row["link"] . "\">" . $row["name"] . "</a></p>";
            }
        } else{
            echo "<p>No matches found</p>";
        }
    }  
} catch(PDOException $e){
    die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}

// Close statement
unset($stmt);

// Close connection
unset($pdo);
?>

我希望当一个人点击结果行中的任意位置时,它会转到链接。目前他们必须点击超链接

【问题讨论】:

  • 在行中放置一个事件监听器。并在其中使用这个指令:window.location = "URL";
  • 我不知道如何添加一个均匀的监听器。你能解释更多吗?
  • EventListener 接口表示一个对象,它可以处理由 EventTarget 对象调度的事件,例如:$('.search-box input[type="text"]').on("keyup input", function(){}) => 当您在输入中按下某个键时,将会处理一些事情。 $(document).on("click","&lt;row selector&gt;",function (){window.location="url"}) => 当您点击元素时,导航将处理
  • 如果仍然不清楚,如果没有人,我会稍后发布答案。

标签: javascript php pdo


【解决方案1】:

这是您应该做什么的粗略示例。运行下面的sn-p查看结果(第二个链接google不通,是stackoverflow的原因,但是如果你自己用的话,就可以了)

$(document).on("click", ".result p", (event) => {
 var url =$(event.target).find("a").prop("href"); // getting the clicked element with event target.
 window.location = url;
})
p {
  width: 100%;
  border: 1px solid red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="result">
  <p><a href="https://stackoverflow.com"> stack</a></p>
  <p><a href="https://google.com"> google</a></p>
</div>

出于各种原因,我将事件绑定在文档上。加载 html (DOM) 后,您的链接将使用脚本创建。使用文档许可在所有“.result p”元素上绑定事件“click”,即使您在页面加载后将其添加到 dom 中也是如此。

$(".result p").click(() => {
   var url =$(this).find("a").prop("href"); 
   window.location = url;
});

它将针对相同的元素。但它只会绑定到您的 html 中已经存在的“.result p”元素。注意:这种方式点击的元素是“this”而不是“event.target”,因为在之前的事件中,“this”指的是“document”。

【讨论】:

    【解决方案2】:

    你可以通过使'a'标签全宽(显示:块)或用'a'覆盖'p'标签来做到这一点,这将使整行可点击。

    【讨论】:

    • 两者都试过了,但是当点击链接时,我在页面上有一个目标 iframe 来显示链接。无论哪种方式,当我单击链接时,搜索框都不会消失
    猜你喜欢
    • 2013-11-13
    • 2011-06-05
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2012-10-27
    相关资源
    最近更新 更多