从php数组自动完成 [英] autocomplete from php array

查看:95
本文介绍了从php数组自动完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码从php数组做自动完成但是它没有用,有人可以帮忙吗?

i wrote this code to do autocomplete from php array but it is not working, can anybody help?

php array

php array

$cars = array("Volvo", "BMW", "Toyota");

我的表格

<form id="frm" method="post">
<input id="cartag" type="text" name="car">
</form>

脚本

$(function() {
var availableTags = [ <?php echo implode(',',$cars); ?>];
	$( "#cartag" ).autocomplete({
	source: availableTags
	});
});

推荐答案

如果你想在jQuery中使用php数组你必须使用 json_encode

if you want to use php array in jQuery you have to user json_encode.

这样:

var availableTags =  <?php echo json_encode($cars); ?>;

工作演示:

<?php

$cars = array("Volvo", "BMW", "Toyota");

?>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="http://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<form id="frm" method="post">
<input id="cartag" type="text" name="car">
</form>
<script>

$(function() {
var availableTags =  <?php echo json_encode($cars); ?>;
    $( "#cartag" ).autocomplete({
    source: availableTags
    });
});

</script>

这篇关于从php数组自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆