从输入文本TypeAhead插件中选择一个值后如何在div内显示图像?

问题描述 投票:0回答:1

我使用的是动态相关的Twitter-TypeAhead,其中,用户在html选择框中选择一个值后,将填充输入文本。当用户在输入文本下拉列表中选择一个选项并使用php或jquery在div中显示此值后,我想获取一个值。实际上,我想在div中显示的值来自产品表“ image”列。

为此,我创建了一个调用products的表,其结构如下:

id | productName    | categoryFK  | image
---------------------------------------------
01 | Nike Tenis     | 1           | 001.jpg
02 | Adidas T-shirt | 2           | 002.jpg
03 | H. Shoes       | 1           | 003.jpg

->台式产品包含一个调用的前键 categoryFK

并显示上面的值,我有一个索引页,其中包含一个用php代码填充的选择框和一个在用户在选择框内选择一个值时用TypeAhead plugin填充的输入文本:

<!DOCTYPE html>
<html lang="en">
<head>

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta charset="utf-8">

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<!-- jQuery UI library -->
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>

</head>
<body>

<div class="container">

<br>

<h1>TWITTER DYNAMIC TYPEAHEAD</h1>

<br>
<div class="row">

<?php 
    // Include the database config file 
    include_once 'dbConfig.php'; 

    // Fetch all the category data 
    $query = "SELECT * FROM categories ORDER BY category ASC"; 
    $result = $db->query($query); 
?>

<!-- category dropdown -->
<div class="col-md-4">
<select id="categoryFK" name="categoryFK" class="form-control">
    <option value="">select category</option>
    <?php 
    if($result->num_rows > 0){ 
        while($row = $result->fetch_assoc()){  
            echo '<option value="'.$row['categoryID'].'">'.$row['category'].'</option>'; 
        } 
    }else{ 
        echo '<option value="">Category not available</option>'; 
    } 
    ?>
</select>
</div>

<div class="col-md-4">

<input type="text" name="products" id="products" class="form-control input-lg" autocomplete="off" placeholder="" />

</div>

<div class="col-md-4">

<div> I would like to display image value here after select a value into input text #products dropdownlist </div>

</div>

</div>
</div>

</body>
</html>

并且在将请求发送到php脚本(fetch.php)的Ajax脚本下面:

<script type="text/javascript">
        var products;

        $ ( function ()
        {
            $('#categoryFK').on('change', function(){
                var queryID = $(this).val();

                $.ajax({
                    url:"fetch.php",
                    method:"POST",
                    data: {
                        category: queryID
                    },
                    dataType:"json",
                    success:function(data)
                    {
                        $("#products").val ('');

                        products = data;
                    }
                });
            });

            $('#products').typeahead({
                source: function ( query, result )
                {
                    result ( $.map(products, function (item)
                        {
                            return item;
                        }
                    ));
                }
            });
        });
        </script>

最后是将响应发送到Ajax的php脚本fetch.php

<?php
include 'dbConfig.php';

if ( isset ( $_POST [ 'category' ] ) ) {
    $request = $db->real_escape_string($_POST["category"]);

    $query = "
        SELECT * FROM products WHERE categoryFK = ".$request."
    ";

    $result = $db->query($query);
    $data = array ();

    if ( $result->num_rows > 0 )
    {
        while($row = $result->fetch_assoc ())
        {
            $data[]=$row["productName"];
        }

        echo json_encode($data);
    }
}
?>

从输入文本#products下拉列表中选择一个值后,如何改进TypeAhead脚本以在div内显示图像值?

javascript php mysql ajax twitter-typeahead
1个回答
1
投票

您可以在像这样的jquery ajax进行更改时做到这一点

HTML和jquery示例

$(document).ready(function() {
    $("#categoryFK").change(function() {
        var src = $(this).val();
        $("#result").html(src ? "<img src='path/to/your/image/" + src + ".jpg'>" : "");
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="categoryFK" name="categoryFK" required>
        <option name="cat-1" value="cat-1">cat-1</option>
        <option name="cat-2" value="cat-2">cat-2</option>
    </select>
<div id="result"></div>
© www.soinside.com 2019 - 2024. All rights reserved.