【发布时间】:2017-04-25 04:20:49
【问题描述】:
我想达到什么目标? 1. 建议列表(可能有相同的标签,但不同的描述) 2. 从该建议列表中选择一个项目时,另一个输入字段的输入应更改为所选项目的相应描述。
当我直接在 javascript 中初始化 var 项目时,它正在工作。但是当我将其更改为从 php 文件中获取建议列表时,只有它的选择部分正在工作,并且基于部分输入的过滤结果停止工作。
有关截图,请访问 github 上的此链接:- https://github.com/rohitdeepu17/BusinessManagement/tree/master/ProjectCode/TestingFiles/Screenshots
代码文件如下:- test_jquery_autocomplete.php
<?php
include 'session_check_common.php';
include 'connect_my_sql_db.php';
?>
<!doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>jQuery UI Autocomplete functionality</title>
<link href = "https://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css"
rel = "stylesheet">
<script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
<script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<style>
#project-label {
display: block;
font-weight: bold;
margin-bottom: 1em;
}
#project-description {
margin: 0;
padding: 0;
}
</style>
<!-- Javascript -->
<script>
$(function() {
/*var projects = [
{
"label": "Java",
"desc": "write once run anywhere"
},
{
"label": "Java",
"desc": "rohit here"
},
{
"label": "jQuery UI",
"desc": "the official user interface library for jQuery"
},
{
"label": "Twitter Bootstrap",
"desc": "popular front end frameworks "
}
];*/
$( "#project" ).autocomplete({
minLength: 0,
//source: projects,
source: "get_customers.php",
focus: function( event, ui ) {
$( "#project" ).val( ui.item.label );
return false;
},
select: function( event, ui ) {
$( "#project" ).val( ui.item.label );
$( "#project-description" ).html( ui.item.desc );
$( "#project-description" ).val( ui.item.desc );
return false;
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.append( "<a>" + item.label + "<br>" + item.desc + "</a>" )
.appendTo( ul );
};
});
</script>
</head>
<body>
<form action="test_autocomplete.php" class="subform" method="post">
<div id = "project-label">Select a project (type "a" for a start):</div>
<input id = "project">
<input id = "project-description" name="projectdescription">
<button type="submit">SUBMIT</button>
</form>
和 get_customers.php
<?php
include 'connect_my_sql_db.php';
$sql="select cust_id, cust_name, father_name from customer";
$cust_name = array();
$father_name = array();
$result=mysqli_query($conn, $sql);
while($row=mysqli_fetch_assoc($result))
{
$title=$row['cust_name'];
$url=$row['father_name'];
$posts[] = array('label'=> $title, 'desc'=> $url);
}
echo json_encode($posts);
?>
【问题讨论】:
标签: javascript php jquery html sql