Jquery UI 自动完成额外参数和自动填充 - 一个令人沮丧的解决方案

发布于 2024-10-10 22:44:01 字数 1765 浏览 0 评论 0原文

一直在研究 Jquery UI 的自动完成 (v1.8.5),并意识到严重缺乏有关发送额外参数和拍摄额外数据以自动填充其他字段的文档。我所拥有的有效,但说真的,看起来像是一个黑客......关于如何改进这个有什么想法吗?

        <script type="text/javascript">

        var optofirst = {

            width: 375,

            // doing "$(this)" here fails

            source: function( request, response ) {

            // grab the calling element
            // "$(this)" here works but ya gotta dig to get to the ID

            var cat = $(this);
            var callid = cat[0].element.context.id; //digging away

            $.ajax({

                // doing "$(this)" here fails

                url: "automagic.php",
                dataType: "json",
                data: {
                term : request.term,

                //send its ID to the php script
                grab : callid,
            },

            success: function( data ) {
                response( $.map( data, function( item ) {
                return {

                // start assigning item handles to the response

                label: item.first,
                value: item.first,
                last: item.last,
                }
                }));
            }
            });
        },
            select: function( event, ui ) {
                console.log( ui.item ?
                "Selected: " + ui.item.last :
                "Nothing selected, input was " + this.value);

                // make #lname have the value last name
                // the "item" in this case appears to get its info from the handles assign in "success:"

                $("#flyover #lname").attr("value",ui.item.last);
            },
        minLength: 2,
        };

        $("#flyover #fname").autocomplete(optofirst);

        </script>

Been looking into Jquery UI's Autocomplete (v1.8.5) and realized there is a sever lack of documentation on sending extra parameters and shooting extra data to autofill other fields. What I have works, but seriously, seems like such a hack... Any thoughts on how to improve this?

        <script type="text/javascript">

        var optofirst = {

            width: 375,

            // doing "$(this)" here fails

            source: function( request, response ) {

            // grab the calling element
            // "$(this)" here works but ya gotta dig to get to the ID

            var cat = $(this);
            var callid = cat[0].element.context.id; //digging away

            $.ajax({

                // doing "$(this)" here fails

                url: "automagic.php",
                dataType: "json",
                data: {
                term : request.term,

                //send its ID to the php script
                grab : callid,
            },

            success: function( data ) {
                response( $.map( data, function( item ) {
                return {

                // start assigning item handles to the response

                label: item.first,
                value: item.first,
                last: item.last,
                }
                }));
            }
            });
        },
            select: function( event, ui ) {
                console.log( ui.item ?
                "Selected: " + ui.item.last :
                "Nothing selected, input was " + this.value);

                // make #lname have the value last name
                // the "item" in this case appears to get its info from the handles assign in "success:"

                $("#flyover #lname").attr("value",ui.item.last);
            },
        minLength: 2,
        };

        $("#flyover #fname").autocomplete(optofirst);

        </script>

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

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

发布评论

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

评论(3

挖鼻大婶 2024-10-17 22:44:01

您可以使用源属性。设置一个函数而不是 url。
您应该只需要编辑 url 并替换要发送到服务器的任意数量的自定义属性的 customData 行。
例如:

        $(this).autocomplete({
            source: function(request, response) {
                $.ajax({
                  url: 'data.php',
                  dataType: "json",
                  data: {
                    term : request.term,
                    customData : $('#something').val()
                  },
                  success: function(data) {
                    response(data);
                  }
                });
            },
            minLength: 3}, {

        }); 

You can use the source property. Set a function instead of an url.
You should need only to edit url and replace customData line for as many custom properties you want to send to the server.
For example:

        $(this).autocomplete({
            source: function(request, response) {
                $.ajax({
                  url: 'data.php',
                  dataType: "json",
                  data: {
                    term : request.term,
                    customData : $('#something').val()
                  },
                  success: function(data) {
                    response(data);
                  }
                });
            },
            minLength: 3}, {

        }); 
靖瑶 2024-10-17 22:44:01

总体想法对我来说看起来不错。您的代码非常接近 jQueryUI 的自定义数据和显示演示

不过,有一些事情您可以改进:

  1. // 在自动完成选项对象和 AJAX 调用中执行“$(this)”都会失败,因为 this在 JavaScript 中,对象字面量没有意义;它包含函数调用的上下文(请参阅这个问题 对此进行了很好的解释);
  2. source 函数中,this 可以工作,因为现在它周围有一个函数。 this 有一个上下文。
  3. 而不是:

    var callid = cat[0].element.context.id; //挖掘
    

    你可以这样写:

    var calid = this.element.attr('id');
    

    这是因为在本例中 this 已经是一个 jQuery 对象。 $(this) 是多余的。此外,element 属性也是一个 jQuery 对象,因此您可以使用 attr()

    访问 id

其余的对我来说看起来没问题。看看我引用的演示——它做了一些与您想要完成的事情类似的事情。

The general idea looks good to me. Your code is pretty close to jQueryUI's custom data and display demo.

There are a few things you could improve on though:

  1. // doing "$(this)" here fails both inside the options object for autocomplete and your AJAX call, because this in JavaScript does not make sense in object literals; it contains the context of a function call (see this question for a great explanation of this);
  2. Inside the source function, this works because now there's a function around it. this has a context.
  3. Instead of:

    var callid = cat[0].element.context.id; //digging away
    

    You could write:

    var calid = this.element.attr('id');
    

    This is because this in this case is already a jQuery object. $(this) is redundant. Also, the element property is also a jQuery object, so you can just access the id using attr()

The rest looks ok to me. Have a look at the demo I referenced--it does some similar things to what you're trying to accomplish.

萌逼全场 2024-10-17 22:44:01

我真的很想在 Rails 3.1 中使用 CoffeeScript 来做到这一点
这是 github 上的要点

I really wanted to do this in rails 3.1 using coffeescript
Heres a gist for it on github

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