jQuery的自动完成:如何设置发送什么服务器? [英] Jquery Autocomplete : how to setup what to send to the server?

查看:126
本文介绍了jQuery的自动完成:如何设置发送什么服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,打字的事情后,服务器将返回我是这样的:

Currently, after typing things the server returns me something like :

[{"id":"1", "value":"My first character"},{"id":"2", "value":"My second Character"}]

,当我选择我的第一个字符,而sumbiting形式发送:

And when i'm selecting "My first character", and sumbiting the form, it sends :

array(1) { ["members"]=> string(18) "My first character" }

但我宁愿发送ID 的。如何判断.autocomplete发送ID,而不是字段的标签?
JS code

But i'd rather send the ID. How to tell .autocomplete to send the id instead of the field's label ? ( JS code )

它必须是简单,但我很想念它。

It must be simple but i'm missing it.

感谢

推荐答案

其实,如果它是你正在使用你将永远提交的显​​示值的文本输入。你可以做的是ID复制到一个隐藏元素的再赶上服务器端。你可以这样做:

Actually if it's a text input you are using you will always submit the displayed value. What you could do is copy the id to an hidden element an then catch that server side. You could do:

            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( ", " );
                    //get the id
                    var id = ui.term.id;
                    //copy it to another hidden element
                    $('#yourhiddeninput').val(id);
                    return false;
            }

这篇关于jQuery的自动完成:如何设置发送什么服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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