jQuery UI autoComplete - 从另一个字段传递一个值

发布于 2024-10-08 01:29:46 字数 502 浏览 0 评论 0原文

我将 jQuery UI 自动完成功能附加到一个有效的联系人字段,但现在我还需要传递一个 company_id (动态)以将搜索不仅限制为搜索字符,还限制为用户之前选择的公司。 IE 如果用户输入“jo”,则所有在 company_id 内包含“jo”的联系人都会被传递。

顺便说一句,remote.php 传回 id 和值,以便我可以填充 contact_name 和 contact_id。那部分有效。

我只是不知道如何将 company_id 字段传递给它。

    $( "#contact_name ).autocomplete({
        source: 'remote.php?t=contactSearch',
        minLength: minlen,
        select: function( event, ui ) {
             $( "#contact_id" ).val(ui.item.id );
        }
    });

谢谢!

I'm attaching jQuery UI autocomplete to a contact field which works, but now I need to also pass a company_id (dynamcially) to restrict the search not only to the search characters but also to a company that the user has previously chosen. IE if the user enter 'jo' all contacts that contain 'jo' that are within the company_id passed.

Incidentally, remote.php passes back id, and value so I can populate both the contact_name and contact_id. That part works.

I just can't figure out how to pass it the company_id field.


    $( "#contact_name ).autocomplete({
        source: 'remote.php?t=contactSearch',
        minLength: minlen,
        select: function( event, ui ) {
             $( "#contact_id" ).val(ui.item.id );
        }
    });

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的十字路口 2024-10-15 01:29:46

经过大量研究和反复试验,这对我有用。

那么这是做什么的:
用户在此代码之前选择了一家公司(未显示),
下面的自动补全附加了 contact_name,并且还传递了 company_id。
remote.php 返回符合部分搜索条件且属于给定公司的联系人的 id 和值的数组。当用户单击某个值时,联系人姓名将被放置在自动完成字段中(没有特殊代码,自动完成会自动完成),并且 id 将被放置在 contact_id 字段中。

        $( "#contact_name" ).autocomplete({
            source: function(request, response) {
                $.getJSON ('remote.php',
                { term: request.term, company_id:$('#company_id').val() }, 
                response );
            },
            select: function( event, ui ) {
                $( "#contact_id" ).val(ui.item.id );
            }
        });


A look at a simplified version of remote.php is:


$myDataRows = array ();
$search = addslashes($_REQUEST['term']);
$company_id = addslashes($_REQUEST['company_id']);

$sql = "SELECT c.contact_id as `id`, contact_name as`value` FROM contacts c
    WHERE c.company_id = '$company_id'  and c.contact_name LIKE '%$search%' ";

$result = mysql_query ($sql);

while ($row = mysql_fetch_assoc($result)) {
   array_push($myDataRows, $row);
}

$ret = json_encode ($myDataRows);

echo $ret;

After much research and trail and error, this works for me.

So what this does:
The user has chosen a company before this code (not shown),
The autocomple below is attached the contact_name, and the company_id is also passed.
remote.php returns an array of both id and value of the contacts that meet the partial search criteria and belong to the given company. When the user clicks on a value, the contact name is placed in the field of the autocomplete(there is no special code for that, autocomplete does that automatically) and id is placed in the contact_id field.

        $( "#contact_name" ).autocomplete({
            source: function(request, response) {
                $.getJSON ('remote.php',
                { term: request.term, company_id:$('#company_id').val() }, 
                response );
            },
            select: function( event, ui ) {
                $( "#contact_id" ).val(ui.item.id );
            }
        });


A look at a simplified version of remote.php is:


$myDataRows = array ();
$search = addslashes($_REQUEST['term']);
$company_id = addslashes($_REQUEST['company_id']);

$sql = "SELECT c.contact_id as `id`, contact_name as`value` FROM contacts c
    WHERE c.company_id = '$company_id'  and c.contact_name LIKE '%$search%' ";

$result = mysql_query ($sql);

while ($row = mysql_fetch_assoc($result)) {
   array_push($myDataRows, $row);
}

$ret = json_encode ($myDataRows);

echo $ret;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文