jquery php json 表单自动填充代码

这种表单自动完成功能,像百度,google,淘宝他们的搜索功能都有,在用户输入一部要的内容时会根据条件显示相关内容。

<script>
;(function($){
/* plugin */
$.fn.autocomplete=function(o){
    if(o.ajax) o.ajax=$.extend({ url:'', datatype:'json', async:true }, o.ajax||{});
    o.elemcss教程=$.extend({ focus:{'color':'#f00'}, blur:{'color':'#000'} }, o.elemcss||{});
    o=$.extend({
        source:null,/* privide an array for match */
        ajax:{},/* provide an ajax conditions, if source is exist this parameter is invalid */
        input:'input',/* provide the selector of input box */
        popup:'ul',/* provide the selector of popup box, it must be a ul element of html */
        elemcss:{}/* provide the focus and blur css objects of items in the popup box */
    },o||{});

    var handler=(function(){
        var handler=function(e,o){ return new handler.prototype.init(e,o); };
        handler.prototype={
            e:null, o:null, timer:null, show:0, $input:null, $popup:null,
            init:function(e,o){
                this.e=e;
                this.o=o;
                this.$input=$(this.o.input,this.e);
                this.$popup=$(this.o.popup,this.e);
                this.initevent();
            },
            match:function(quickexpr,value,source){
                for(var i in source){
                    if( value.length>0 && quickexpr.exec(source[i])!=null )
                        this.$popup.append('<li><a href="网页特效:;">'+source[i]+'</a></li>');
                }
                if($('li a',this.$popup[0]).length){ this.$popup.show(); }else{ this.$popup.hide(); }
            },
            fetch:function(ajax,search,quickexpr){
                var that=this;
                $.ajax({
                    url: ajax.url+search,
                    datatype: ajax.datatype,
                    async: ajax.async,
                    error: function(data,es,et){ alert('error'); },
                    success: function(data){ that.match(quickexpr,search,data); }
                });
            },
            initevent:function(){
                var that=this;
                this.$input.focus(function(){
                    var value=this.value, quickexpr=regexp('^'+value,'i'), self=this;
                    that.timer=setinterval(function(){
                        if(value!=self.value){
                            value=self.value;
                            that.$popup.html('');
                            if(value!=''){
                                quickexpr=regexp('^'+value);
                                if(that.o.source) that.match(quickexpr,value,that.o.source);
                                else if(that.o.ajax) that.fetch(that.o.ajax,value,quickexpr);
                            }
                        }
                    },200);
                }).blur(function(){
                    clearinterval(that.timer);
                    var current=-1;
                    var len=that.$popup.find("li a").length-1;
                    $("li a",that.$popup[0]).click(function(){
                        that.$input[0].value=$(this).text();
                        that.$popup.html('').hide();
                    }).focus(function(){
                        current = $(this).parent().index();
                        $(this).css(that.o.elemcss.focus);
                    }).blur(function(){
                        $(this).css(that.o.elemcss.blur);
                    });
                    $("li a",that.$popup[0]).keydown(function(event){
                        if(event.keycode==40){
                            current++;
                            if(current<0) current=len;
                            if(current>len) current=0;
                            that.$popup.find("li a").get(current).focus();
                        }else if(event.keycode==38){
                            current--;
                            if(current>len) current=0;
                            if(current<0) current=len;
                            that.$popup.find("li a").get(current).focus();
                        }
                    });
                }).keydown(function(event){
                    if(event.keycode==40){
                        that.$popup.blur().find("li a").get(0).focus();
                    }
                });
                $(this.e).hover(function(){ that.show=1; },function(){ that.show=0; });
                $(document).click(function(){ if(that.show==0){ that.$popup.hide(); } });
            }
        };
        handler.prototype.init.prototype=handler.prototype;/* jquery style */
        return handler;
    })();

    return this.each(function(){ handler(this,o); });
};
/* invoke */
$(document).ready(function(){
    $(".autocomplete").autocomplete({
        source:[123,1234,43563,12346,3464564,3454,7567,956,456,9383,893,999],
        //ajax:{ url:'./php教程/fetch.php?search=', datatype:'json', async:false },
        elemcss:{ focus:{'color':'#0f0'}, blur:{'color':'#f00'} }
    });
});
/* conclude */
})(jquery);

</script>

php代码

<?php
class datafetch{   
private $conn, $rs;   
function __construct(){       
$this->conn = mysql教程_connect("localhost","root","pwd") or die("cant't connect host~");        mysql_select_db("studentinfo",$this->conn) or die("cant't select database~");    }   
public function data_list($s){       
$this->rs=mysql_query("select sno from student where sno like '$s%'") or die("can't fetch~");       
$datalist=array();       
while($row = mysql_fetch_array($this->rs,mysql_assoc))
{               
array_push($datalist,$row);      
 }       
 return $datalist;    }}
 $search= isset($argv[1]) ? $argv[1] :   ( isset($_get['search']) ? $_get['search'] : '' ) ;
 $fetch=new datafetch();
 $data=$fetch->data_list($search);
 echo '[';
 foreach ($data as $key=>$value)
 {    echo "nt";   
 echo '"'.$value['sno'].'"';   
 if( $key!=count($data)-1 ) echo ','; else echo "n";}echo "]n";
 ?>
 

 这里是利用了php mysql返回json数据,因为上面的jquery也是分析json数据来处的,因为json数据比较实时小而快,所以很多时候都用它了。

时间: 2024-08-04 08:15:11

jquery php json 表单自动填充代码的相关文章

chrome表单自动填充导致input文本框背景变成偏黄色问题解决

chrome表单自动填充后,input文本框的背景会变成偏黄色的,这是由于chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性,然后对其赋予以下样式:   复制代码 代码如下: input:-webkit-autofill { background-color: #FAFFBD; background-image: none; color: #000; } 在有些情况下,这个黄色的背景会影响到我们界面的效果,尤其是在我们给input文本框使用图片背

chrome浏览器当表单自动填充时如何去除浏览器自动添加的默认样式_javascript技巧

一.发现该问题的原因-是在写账号登录页面时,input表单添加了背景图片,当自动填充,搓搓的一坨淡黄色背景出来. 这个原因是我草率的直接设置在input元素里面,结果问题就来了.所以如果把这个图标放在input表单外面,就不会出现这个问题. 二.表单自动填充会添加浏览器默认样式怎么处理和避免 第二张图,就是表单自动填充后,chrome会默认给自动填充的input表单加上input:-webkit-autofill私有属性 input:-webkit-autofill, textarea:-web

360极速浏览器如何设置表单自动填充

  360极速浏览器设置表单自动填充的方法如下: 如果已经厌倦了反复填写信息相同的网络表单,请使用只需点击一次即可完成表单填写的自动填充功能,从而节省时间并减少键入内容.如果不想使用自动填充功能,则可以在"自动填充选项"对话框中停用该功能.

Jquery练习之表单验证实现代码_jquery

Jquery练习表单验证 复制代码 代码如下: <body> <form action="" method="post" id ="myform"> <table> <tr> <td>姓名:</td> <td><input type ="text" id = "name" name ="name"&

JSp页面传值和Ext JS表单自动填充

问题描述 有一个JSP文件:Center_right.jsp<body><table border ="1"> <tr><td>题目编号</td><td>题目内容</td><td>题目类型</td><td>选项个数</td><td>选 项</td><td>题目备注</td><td>操作</t

jquery插件制作 表单验证实现代码_jquery

先下页面代码: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>&l

jquery序列化form表单使用ajax提交后处理返回的json数据

 这篇文章主要介绍了jquery序列化form表单,使用ajax提交后处理返回的json数据的示例,需要的朋友可以参考下 1.返回json字符串:    代码如下: /** 将一个字符串输出到浏览器 */     protected void writeJson(String json) {         PrintWriter pw = null;         try {             servletResponse.setContentType("text/plain;cha

jQuery实现form表单元素序列化为json对象的方法_jquery

本文实例讲述了jQuery实现form表单元素序列化为json对象的方法.分享给大家供大家参考,具体如下: 这段代码序列化form表单元素为json对象: <!Doctype html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>jQuery扩展--form序列化到json对象</title> <meta http-equiv=Content-Type content=&

禁用Enter键表单自动提交实现代码_javascript技巧

复制代码 代码如下: //禁用Enter键表单自动提交 document.onkeydown = function(event) { var target, code, tag; if (!event) { event = window.event; //针对ie浏览器 target = event.srcElement; code = event.keyCode; if (code == 13) { tag = target.tagName; if (tag == "TEXTAREA"