我需要用ajax返回ID jQuery的自动完成功能的expample和名称 [英] I need an expample of jQuery Autocomplete returning id and name using ajax

查看:174
本文介绍了我需要用ajax返回ID jQuery的自动完成功能的expample和名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的例子如何code一个jQuery自动完成填充的product_id同时显示PRODUCT_NAME调用一个AJAX页面remote.php

I need an example of how to code a jQuery autocomplete to populate product_id while showing the product_name calling an ajax page "remote.php"


<input name="product_name" id="product_name" type="text" value="" />
<input name="product_id" id="product_id" type="hidden" value="" />


remote.php:

$partial = addslashes($_POST['partial_search']);
$myDataRows = array(); 
$result = mysql_query ("SELECT product_id, product_name FROM products 
   WHERE product_name like "%$partial%");
while ($row = mysql_fetch_row($result)) {
   array_push($myDataRows, $row);
}
$ret = json_encode ($myDataRows);
echo $ret;

我不知道如何code jQuery的自动完成,如果我需要改变remote.php

I'm not sure how to code the jQuery autocomplete and if I need to change remote.php

感谢

后来补充:

我摸索出另一种解决方案:

I worked out another solution:


<script type="text/javascript">
function nqi_search (type, id_name, text_name)
{
    $( "#"+text_name ).autocomplete({
        source: "remote.php?&t="+type,
        minLength: 1,
        select: function( event, ui ) {
            $( "#"+id_name ).val(ui.item.id );
        }
    });
}
</script>

<script type="text/javascript">
jQuery(document).ready(function() {

    nqi_search ("product_search", "product_id", "product_name");

    // also you can have many on one page with:
    nqi_search ("vendor_search", "vendor_id", "vendor_name");   


});
</script>

还有一个问题。它似乎并不如nqi_search功能被放入一个.js文件工作。我不知道为什么?

There's one problem. it doesn't seem to work if the nqi_search function is put into a .js file. I have no idea why?

推荐答案

这是我如何做到这一点:

This is how I do it:

请注意,我有codeD的特殊功能,其中JSON可以标记一个消息,而不是在这样就可以把信息列表(例如我把一个新加入的X未显示的物品的项目为长列表)。要使用邮件功能,但在标签字段中的文本和邮件领域的真正布尔值。

Note, I've coded a special feature where the json can flag an item as a message instead and in this way you can put messages in the list (eg I put a "Addition X items not shown" for long lists). To use the message feature, but the text in the label field and a true boolean for the message field.

要在页面上使用此我只是有

To use this on the page I just have

setupAutocomplete(<id of textbox>,<path to service>);


  $.ajaxSetup({
      type: "POST",
      contentType: "application/json; charset=utf-8",
      data: "{}",
      dataFilter: function(data) {
         var msg;

         if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
            msg = JSON.parse(data);
         else
            msg = eval('(' + data + ')');

         if (msg.hasOwnProperty('d'))
            return msg.d;
         else
            return msg;
      },
      error: function(msg) {
         $('#error').html(msg.responseText)
      }
   });


// remove this to get rid of custom message handling
$.widget("custom.redcomplete", $.ui.autocomplete, {
   _renderMenu: function(ul, items) {
      var self = this;
      $.each(items, function(index, item) {

         if (item.message)
            ul.append("<li class='ui-menu-item special-red'>&nbsp;" + item.label + "</li>");
         else
            self._renderItem(ul, item)
      });
   }


function setupAutocomplete(inID, inURL) {
   var myTB = $("[id$='_" + inID + "']");
   // change redcomplete to autocomplete to get rid of message handling
   myTB.redcomplete({
      source: function(request, response) {
         $.ajax({
            url: inURL,
            data: "{'filter': '" + request.term + "'}",
            success: function(data) {
               response($.map(data, function(item) {
                  return {
                     label: item.text,
                     value: item.id,
                     // remove this line and the , above to get rid of message handling
                     message: item.message
                  };
               }));
            }
         })
      },
      delay: 500,
      minLength: 3,
      focus: function(event, ui) {
         myTB.val(ui.item.label);
         return false;
      },
      select: function(event, ui) {
        // action for the select here.
         return false;
      },
      open: function() {
         $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
      },
      close: function() {
         $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
      }

   });

这篇关于我需要用ajax返回ID jQuery的自动完成功能的expample和名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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