JQuery serialize后,在后台取不到值的问题

来源:互联网 发布:银行叫号系统c语言 编辑:程序博客网 时间:2024/04/27 23:43

在使用JQuery的AutoComplete功能时,出现一个奇怪的问题,那就是后台取不到被serialize后的form中的input的值。为这个问题烦恼了n久,终于找到原因。原因很汗。。。那就是被serialize的form被嵌套在了另外一个form中。目前只是知道如何修改,至于原因,暂时不考虑,留待有空验证。顺便记录一下JQueryAutoComplete的用法(框架是Thinkphp3.2)。

前台:

<html>

<head>

</head>

<body>

<link href="__PUBLIC__/Home/jquery-ui/css/ui-lightness/jquery-ui-1.8.17.custom.css" rel="stylesheet" />
<script src="__PUBLIC__/Home/jquery-1.7.1.min.js"></script>
<script src="__PUBLIC__/Home/jquery-ui/js/jquery-ui-1.8.17.custom.min.js"></script>
<script src="__PUBLIC__/Home/jquery-ui/js/jquery-ui-widget-combobox.js"></script>
<script type="text/javascript">
  //getgoodstrend ajax end
var Auto = $.noConflict();   //使用了AmazeUI的前段框架,里面也引用了另外一个版本的JQuery,此处意思是让JQuery知道用了两个库,他会去找最合适的库,不会出现方法不存在的问题
//auto complete start
Auto(function() {
    Auto("#goodsidtoadd").autocomplete({
    source:function( request, response ) {
      Auto.ajax({
        url: "__URL__/searchgoodstoaddbyajax",
            dataType: "json",
            data:$('#addgoodsform').serialize(),
            success: function( result ) {
              response( Auto.map( result, function( item ) {
                    return item;
                }));
            }

      });
    }
    });
});
//auto complete end
</script>

<form name="addgoodsform" accept-charset="utf-8" id="addgoodsform" method="post">

 <div class="am-u-sm-2">药品名称:</div>
        <input type="text" name="goodsidtoadd" id="goodsidtoadd" placeholder="药品名称" class="am-form-field">                 
  </div>

</form>

</body>

</html>


后台:

 public function searchgoodstoaddbyajax()
    {
        
        $SICK = M("sickness");
        $queryStr = "name LIKE '%".I("get.goodsidtoadd")."%' OR id LIKE '%".I("get.goodsidtoadd")."%'";
        $sicklist = $SICK->where($queryStr)->select();  
        $index = 0;
        foreach ($sicklist as $sick) {
            $json[$index]['label']=$sick['name'];
            $json[$index]['value']=$sick['id'];
            $index++;
        }
        if(empty($json))
        {                         
            $json[0]['label']="无对应病症";
            $json[0]['value']="";
        }
        $this->ajaxReturn($json,'JSON');
        /*
        $CON = M("vipconsumption");
        $queryStr = "goodsname LIKE '%".I("get.searchinfo")."%' OR goodscode LIKE '%".I("get.searchinfo")."%'";
        $conlist = $CON->where($queryStr)->group("goodscode")->field("goodscode,goodsname")->limit(10)->select();  
        $index = 0;
        foreach ($conlist as $con) {
            $json[$index]['label']=$con['goodsname'];
            $json[$index]['value']=$con['goodscode'];
            $index++;
        }
        if(empty($json))
        {                         
            $json[0]['label']="无对应药品";
            $json[0]['value']="";
        }
        $this->ajaxReturn($json,'JSON');
        */
    }


效果

0 0