第一个框自动填充用户键入并选择其中一个选项。该值来自具有3个属性的数据库:id
,name
和value
。我希望第一个文本框中所选名称的value
自动显示在第二个框中。在用户选择value
后,如何随身携带name
name
?
的index.php:
<script type="text/javascript">
$(document).ready(function()
{
$('#auto').autocomplete(
{
source: "search.php?json",
minLength: 3
});
});
</script>
</head>
<body>
<p>Type the name of a band: <input type="text" id="auto" /></p>
</body>
</html>
search php file
<?php
$mysqli = taken off;
$text = $mysqli->real_escape_string($_GET['term']);
$query = "SELECT name, salary FROM tag WHERE name LIKE '%$text%' ORDER BY
name ASC";
$result = $mysqli->query($query);
$json = '[';
$first = true;
while($row = $result->fetch_assoc())
{
if (!$first) { $json .= ','; } else { $first = false; }
$json .= '{"value":"'.$row['name'].'"}';
}
$json .= ']';
echo $json;
?>
答案 0 :(得分:0)
尝试通过执行类似的操作在自动填充中附加触发器
$( '#auto' ).autocomplete({
select: function( event, ui ) {
var myValue = $(this).val();
//Do what you want with your value
}
});
答案 1 :(得分:0)
这很有用。
<script type="text/javascript">
$(document).ready(function()
{
$('#auto').autocomplete(
{
source: "search.php?json",
minLength: 3,
close: function( event, ui ) { getSalary(document.getElementById('auto').value) },
change: function( event, ui ) { getSalary(document.getElementById('auto').value) },
select: function( event, ui ) { getSalary(document.getElementById('auto').value) }
});
});
function getSalary(name_salary){
$.post( "salary.php", { term: name_salary }, function(data) {
var test = JSON.parse(data);
document.getElementById('autosalary').value = test.value;
$( "#mincome" ).text(test.value);
});
}
</script>