栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

jQuery UI使用JSON和Ajax自动完成

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

jQuery UI使用JSON和Ajax自动完成

您可以非常坚持jQueryUI的自动完成功能的远程演示:http : //jqueryui.com/resources/demos/autocomplete/remote-
jsonp.html

为了使结果进入自动完成列表,您需要在ajax成功函数中放置一个带有标签和响应参数(实际上是一个函数)值的对象:

source: function( request, response ) {    $.ajax({        url: "fill_id.php",        data: {term: request.term},        dataType: "json",        success: function( data ) { response( $.map( data.myData, function( item ) {     return {         label: item.title,         value: item.turninId     } }));        }    });}

但这仅在您稍微修改fill_id.php时有效:

// escape your parameters to prevent sql injection$param   = mysql_real_escape_string($_GET['term']);$options = array();// fetch a title for a better user experience maybe..$db = new SQLite3('database/main.db');    $results = $db->query("SELECt distinct(turninId), title FROM main WHERe turninid LIKE '".$param."%'");while ($row_id = $results->fetchArray()) {    // more structure in data allows an easier processing    $options['myData'][] = array(        'turninId' => $row_id['turninId'],        'title'    => $row_id['title']    ); }// modify your http header to json, to help browsers to naturally handle your response withheader('Cache-Control: no-cache, must-revalidate');header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');header('Content-type: application/json');echo json_enpre($options);

当然,当您的表中没有标题或任何内容时,也可以仅保留响应,并在成功回调中重复id。重要的是,您需要

response
在自动完成功能中使用值/项目对来填充功能:

// this will probably work without modifying your php file at all:response( $.map( data, function( item ) {    return {        label: item,        value: item    }}));

编辑:将引用链接更新为新的jquery UI的自动完成ui



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392171.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号