【问题标题】:Jquery autocomplete not updating the input value with selectionJquery自动完成不使用选择更新输入值
【发布时间】:2012-04-21 11:46:30
【问题描述】:

我正在尝试使用 jquerys 著名的自动完成功能并通过从 mysql 数据库创建一个数组来创建一个自动完成字段。它工作得很好,但是当进行选择时,输入字段值不会更新,因此我无法从表单中传递值。有人可以帮我吗?

JQUERY:

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css " type="text/css" media="all" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js " type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/jquery-ui.min.js " type="text/javascript"></script>
<script>
$(document).ready(function() {
  $("#keywords").autocomplete({
    source: keywordList
  });
});

</script>
<?php echo keywordArray(); ?>

PHP:(为自动完成创建数组列表)

<?php
include 'admin/dbconn-dest.php';

function keywordArray()
{
  $rsKeywords = mysql_query("SELECT Destination FROM Destinations WHERE Country = 'Mexico'");

  $output = '<script>'."\n";
  $output .= 'var keywordList = [';

  while($row_rsKeywords = mysql_fetch_assoc($rsKeywords))
  {
    $output .= '"'.$row_rsKeywords['Destination'].'",';
  }

  $output = substr($output,0,-1); //Get rid of the trailing comma
  $output .= '];'."\n";
  $output .= '</script>';
  return $output;
}
?>

HTML:

<input id="keywords" name="keywords" type="text" autocomplete="off" size="40" >

任何帮助将不胜感激!!!

【问题讨论】:

  • 你能发一个jsfiddle来演示这个问题吗?

标签: php jquery mysql input autocomplete


【解决方案1】:

使用select event设置输入框的值

$("#keywords").autocomplete({
    source: keywordList,
    select: function (event, ui) {
                         $("#keywords").val(ui.item.value);                       
                     }  
  });

【讨论】:

    【解决方案2】:

    这是一个已知问题。尝试使用Select First 插件。这应该为你完成工作(至少在我的情况下它工作得很好)

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 2012-07-08
      • 1970-01-01
      相关资源
      最近更新 更多