使用extraParams传递附加的GET变量实现jQuery自动完成

28

我特别指的是Jörn Zaefferer的jQuery Autocomplete v1.1插件[来源:http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/],因为这个插件似乎有很多版本。

我试图在用户开始输入时向服务器传递其他参数,因为我有多个字段需要自动完成提供建议。

除了查询之外,我还想将输入名称属性发送到服务器,但我似乎无法在extraParams中使用$(this).attr('name')。

我的jQuery:

   $('.ajax-auto input').autocomplete('search.php', {
     extraParams: {
      search_type: function(){
       return $(this).attr('name');
      }
     }
   })
这是我的 HTML。
 <form method="post" action="#" id="update-form" autocomplete="off">
  <ol>
         <li class="ajax-auto">
             <label for="form-initials">Initials</label>
                <input type="text" id="form-initials" name="initials" />
            </li>
         <li class="ajax-auto">
             <label for="form-company">Company</label>
                <input type="text" id="form-company" name="company" />
            </li>
  </ol>
 </form>

有什么建议吗?

13个回答

0

我不确定为什么它不起作用。

但你可以先检查/调试$(this).attr('name')的值。

还有一件事,就像这里解释的[在选项卡中],你可以使用Firebug来查看ajax post请求(url和数据),这将帮助你解决问题。


0

我也遇到了同样的问题,但奇怪的是,只有在使用自动完成插件的压缩版本时才会出现。当我使用未压缩的版本时,它可以正常工作。我还没有查看压缩版本以确定差异在哪里。


0

我知道这个问题已经有答案了。但我希望这篇文章能够帮助到未来的某个人,节省时间和痛苦。

(你可以根据你的jQuery版本将'CRM.$'替换为'$'或'jQuery')

完整的代码如下:我为一个文本框编写了自动完成功能,希望能帮到别人。

CRM.$( 'input[id^=custom_78]' ).autocomplete({
            autoFill: true,
            select: function (event, ui) {
                    var label = ui.item.label;
                    var value = ui.item.value;
                    // Update subject field to add book year and book product
                    var book_year_value = CRM.$('select[id^=custom_77]  option:selected').text().replace('Book Year ','');
                    //book_year_value.replace('Book Year ','');
                    var subject_value = book_year_value + '/' + ui.item.label;
                    CRM.$('#subject').val(subject_value);
                    CRM.$( 'input[name=product_select_id]' ).val(ui.item.value);
                    CRM.$('input[id^=custom_78]').val(ui.item.label);
                    return false;
            },
            source: function(request, response) {
                CRM.$.ajax({
                    url: productUrl,
                        data: {
                                        'subCategory' : cj('select[id^=custom_77]').val(),
                                        's': request.term,
                                    },
                    beforeSend: function( xhr ) {
                        xhr.overrideMimeType( "text/plain; charset=x-user-defined" );
                    },

                    success: function(result){
                                result = jQuery.parseJSON( result);
                                //console.log(result);
                                response(CRM.$.map(result, function (val,key) {
                                                         //console.log(key);
                                                         //console.log(val);
                                                         return {
                                                                 label: val,
                                                                 value: key
                                                         };
                                                 }));
                    }
                })
                .done(function( data ) {
                    if ( console && console.log ) {
                     // console.log( "Sample of dataas:", data.slice( 0, 100 ) );
                    }
                });
            }
  });

PHP代 码,演示如何将数据返回给此jquery自动完成的ajax调用:

/**
 * This class contains all product related functions that are called using AJAX (jQuery)
 */
class CRM_Civicrmactivitiesproductlink_Page_AJAX {
  static function getProductList() {
        $name   = CRM_Utils_Array::value( 's', $_GET );
    $name   = CRM_Utils_Type::escape( $name, 'String' );
    $limit  = '10';

        $strSearch = "description LIKE '%$name%'";

        $subCategory   = CRM_Utils_Array::value( 'subCategory', $_GET );
    $subCategory   = CRM_Utils_Type::escape( $subCategory, 'String' );

        if (!empty($subCategory))
        {
                $strSearch .= " AND sub_category = ".$subCategory;
        }

        $query = "SELECT id , description as data FROM abc_books WHERE $strSearch";
        $resultArray = array();
        $dao = CRM_Core_DAO::executeQuery( $query );
        while ( $dao->fetch( ) ) {
            $resultArray[$dao->id] = $dao->data;//creating the array to send id as key and data as value
        }
        echo json_encode($resultArray);
    CRM_Utils_System::civiExit();
  }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接