0

我正在使用自动完成

http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/

下面我有两页index.phpajx_page.php

索引.php

<form name="send_coup_ajx_form" id="send_coup_ajx_form" method="post">

    <select name="member_type" id="member_type">
        <option value="customer_type">Customer</option>
        <option value="BPartner_type">Business Partner</option>                
    </select>

    <input type="text" name="sel_rad" id="resultant_text" />

    <input type="submit" name="submit" value="go" />

</form>


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


        $("#resultant_text").autocomplete("ajx_page.php", {
            width: 260,
            matchContains: true,
            //mustMatch: true,
            //minChars: 0,
            //multiple: true,
            //highlight: false,
            //multipleSeparator: ",",
            selectFirst: false
        });

    });

</script>

下面的代码

ajx_page.php

<?php
    $select_type = $_GET['member_type'];
    $type = $_GET['sel_rad'];
?>

当我在文本框中输入时,它给了我一个错误

Notice: Undefined index: member_type in ajx_page.php on line 2

这意味着member_type不会进入该页面..

当我评论所有这些代码并将下面的代码放入ajx_page.php

<?php
    echo "Name1\n";
    echo "Name2\n";
    echo "Name3\n"
?>

它显示自动完成功能列表。但是当我想获取该对象的值时,member_type它说的是一个错误..

那么我如何使用这个自动完成插件传递额外的值

我已将图像添加到 @veeTrain 覆盖问题

在此处输入图像描述

4

2 回答 2

1

我认为为 bassistance 的自动完成传递附加参数,唯一的方法是修改 autocomplete.js 文件。我以前做过类似的事情,但是方法是相当硬编码的。贴在下面供您参考(大约在第 376 行):

$.ajax({
    // try to leverage ajaxQueue plugin to abort previous requests
    mode: "abort",
    // limit abortion to this input
    port: "autocomplete" + input.name,
    dataType: options.dataType,
    url: options.url,
    data: $.extend({
        member_type: ($("#member_type").length != 1)?$("#member_type").val():-1,
        q: lastWord(term),
        limit: options.max
    }, extraParams),
    success: function(data) {
        var parsed = options.parse && options.parse(data) || parse(data);
        cache.add(term, parsed);
        success(term, parsed);
    }
});

我只在原始代码中添加了一行,在第 10 行:member_type: ...。请注意,如果在该页面上找不到该对象,我会传入“-1”以防止 javascript 错误。希望能帮助到你!

于 2012-04-27T12:50:23.543 回答
1

我不确定自动完成插件是否旨在处理这种类型的场景。

但是,如果您想手动将这些值传递到您的 php 脚本中,那么它会非常简单(至少在这种情况下,您只尝试发送两个字段)。

var memberType = $("#member_type>option:selected").val();
var selRad = $("#resultant_text").val();
$("#resultant_text").autocomplete("ajx_page.php?member_type="+memberType+"&sel_rad="+selRad, {
//...

但这可能不是您正在寻找的答案。

更新:我很高兴发送值的语法对您有用。

看起来你有重叠的 id 被打印出来——这就是你所说的“粗体”吗?显然,您没有使用正确的分隔符来创建第二个自动完成建议。我不确定自动完成处理程序想要在项目之间描述什么。未定义可能会出现,因为找不到您的项目之一。您应该使用浏览器的开发人员工具来调试发送的内容。

此外,如果您想要选项的文本而不是 the,value那么您需要访问select 下拉菜单的.text()而不是 the 。.val()

更新这篇文章可能对自动完成的期望有什么要说的。

于 2012-04-27T13:05:00.457 回答