简体   繁体   English

如何选择 <li> 通过将鼠标悬停并在 <ul> 元件

[英]How to select <li> element by hovering and clicking over them in a <ul> element

I am trying to make a live search for the products of a specified supplier. 我正在尝试实时搜索指定供应商的产品。

I have an input element with the Supplier Name to be entered in it: 我有一个输入元素 ,其中要输入供应商名称:

<input title="supplier">

I have the following Input Element and Un-Ordered List Element 我有以下输入元素无序列表元素

Live Product Search <input type="search" name="search" id="search">
<ul id="here" style="position: fixed; background-color: white; width:175px; height:300px; border: 1px solid grey; display:none; padding:0px 0px 10px 10px; "></ul>

I am populating ul element dynamically with live list elements from database using JQuery $.ajax and JQuery .keyup() function. 我正在使用JQuery $ .ajax和JQuery .keyup()函数使用数据库中的活动列表元素动态填充ul元素

Javascript and JQuery for this purpose is: 为此目的,JavaScript和JQuery是:

$(document).ready(function(){
    $("#search").keyup(function(){
var y=$('[title="supplier"]').val();        
        $("#here").show();
        var x =$(this).val();
        $.ajax({
            type: 'GET',
            url:'getdataforproductslive.php',
            data:{q: x, s: y},  
            success:function(p)
            {
var pr= p.split("|");
for (var option in pr){
var newLi = document.createElement("li");
newLi.innerHTML=pr[option];
{$("#here").append(newLi);}}},
});
$("#here").html("");
});
})
$("#search").blur(function(){$("#here").hide();})

This whole setup is populating the ul element with the product names as li elements , but I do not know how to select one of these products or li elements and pass it to another input element in another table, which is supposed to use the product name . 整个设置将使用产品名称作为li元素填充ul 元素但是我不知道如何选择这些产品或li元素之一并将其传递给另一个表中的另一个输入元素,该表应该使用产品名

Can anyone guide me as to how to do this properly? 谁能指导我如何正确执行此操作?

  var sampleData = ['Product 1', 'Product 2', 'Product 3']; function setSupplier() { $('#supplier').val($(this).text()); } $("#search").on('input keyup', function () { var val = $(this).val().trim(); if (!val.length) { $("#here").empty().hide(); return; } $.ajax({ type: 'GET', url: 'http://echo.jsontest.com/key/value/one/two', data: { q: val }, success: function (res) { $("#here").empty().show(); if ($("#here").data('state') === 'focusout') { $("#here").hide(); return; } var items = sampleData.slice(); for (var i = 0; i < items.length; i++) { var li = $(document.createElement("li")).html(items[i] + ' ( ' + val + ' )').appendTo("#here"); li.on('mousedown', setSupplier); } } }); }).on('focusin', function () { if ($("#here").children().length) { $("#here").show(); } $("#here").data('state', 'focusin'); }).on('focusout', function () { if (!$(this).val().trim().length) { $("#here").empty(); } $("#here").data('state', 'focusout').hide(); }); 
 #here li:hover { background: #ccc; cursor: pointer; } #here { list-style-type: none; padding: 5px 10px; } 
 <input type="search" name="search" id="search" placeholder="Type a supplier name"> <input type="input" id="supplier" style="margin-top: 20px" readonly> <ul id="here" style="position: fixed; background-color: white; width:175px; height:300px; border: 1px solid grey; display:none;"></ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 

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

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