【问题标题】:JQuery UI Autocomplete Send Extra Term By JSONJQuery UI 自动完成通过 JSON 发送额外的术语
【发布时间】:2012-12-18 15:10:54
【问题描述】:

我从 jQuery UI 文档中获取了大部分代码。 我想添加到请求变量中,以便同时发送<input> id。

输入如下所示:

<input size="50" class="attr_values" name="msrp" id="msrp" />

现在这里是 jquery。在 .autocomplete 源 $.getJSON 内部,我尝试使用“this.id”,但它不起作用。我怎样才能让它工作?

$(document).ready(function() { 
    // 2 string parsing functions
    function split( val ) {
        return val.split( /,\s*/ );
    }
    function extractLast( term ) {
        return split( term ).pop();
    }


    $( '.attr_values' )
        // don't navigate away from the field on tab when selecting an item
        .bind( 'keydown', function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB &&
                    $( this ).data( 'autocomplete' ).menu.active ) {
                event.preventDefault();
            }
        })
        .autocomplete({
            source: function( request, response ) {
                $.getJSON( 'controllers/core_data/ajax/core_data.php', {
                    'attr_name' : this.id, // THIS DOESN'T WORK
                    'term': extractLast( request.term )
                }, response );
            },
            search: function() {
                // custom minLength
                var term = extractLast( this.value );
                if ( term.length < 2 ) {
                    return false;
                }
                // var attr_name = this.id;
            },
            focus: function() {
                // prevent value inserted on focus
                return false;
            },
            select: function( event, ui ) {
                var terms = split( this.value );
                // remove the current input
                terms.pop();
                // add the selected item
                terms.push( ui.item.value );
                // add placeholder to get the comma-and-space at the end
                terms.push( '' );
                this.value = terms.join( ', ' );
                return false;
            }
        });

 });

还请注意,我已将其应用于“.attr_values”类,而不是像示例所示的那样应用于一个特定的 id。我有一大堆字段,每个字段都有不同的 id。这就是为什么我需要将 id 和术语发送到 php 脚本。这样它就知道要在哪个表中查找该术语。

【问题讨论】:

  • 抱歉,我在顶部的标题有错误。修好了。

标签: jquery jquery-ui jquery-autocomplete


【解决方案1】:

thisgetJSON 回调中不再引用原始集合。尝试向上钻取以到达原始元素:

source: function( request, response ) {
    $.getJSON( 'controllers/core_data/ajax/core_data.php', {
        'attr_name' : this.element[0].id,
        'term': extractLast( request.term )
    }, response );
},

这里的上下文this 指的是您正在为其评估source 函数的autocomplete 对象。我们可以使用element属性获取对应的jQuery集合,然后从中找到id。

【讨论】:

  • 看起来很酷,但“this.id”仍然无法正常工作。事实上,现在似乎根本没有数据被发送到“core_data.php”。
  • @ButtleButkus 我做了一些改变。立即尝试。
  • 谢谢。它工作得非常出色。所需的数据终于通过了 php 脚本。我留下的唯一问题是为什么 Google Chrome 调试器在“源”部分的右括号上暂停。有什么想法吗?
  • 显然我确实这样做了。谢谢!顺便说一下,我马上要问另一个问题。
【解决方案2】:

试着给这个..

$(this).attr('id');

【讨论】:

    猜你喜欢
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    • 2013-08-28
    • 1970-01-01
    • 2018-02-16
    • 2018-01-18
    相关资源
    最近更新 更多