jQuery的的UI自动完成本地工作不是在Heroku [英] jquery-ui autocomplete works locally not on heroku

查看:164
本文介绍了jQuery的的UI自动完成本地工作不是在Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用的应用程序西纳特拉HTML表单的两个输入字段的jQuery的UI的自动完成。他们在我的本地计算机上,但不能在Heroku工作。 jQuery的-UI的日期选择器的工作原理是同一页上,所以我知道的JavaScript文件被正确加载了jQuery和jQuery的UI。另外,我可以看到我使用作为源自动完成的变量实际上得到从数据库中填充。有没有控制台错误也是如此。

I am using jquery-ui's autocomplete on two input fields of an HTML form in a Sinatra app. They work on my local machine but not on Heroku. Jquery-ui's datepicker works on this same page, so I know the jquery and jquery-ui javascript files are being loaded correctly. Also, I can see that the variable that I am using as the source for the autocomplete is actually getting populated from the database. There are no console errors as well.

这里是HTML:

<form action="/add-event" method="post">
     Date: <input id="datepicker" class="required" type="text" maxlength="50" name="date" /><br />
     Headliner: <input id="headliner" class="required" type="text" maxlength="50" name="headliner" /><br />
     Venue: <input id="venue" class="required" type="text" maxlength="50" name="venue_name" /><br />
</form>

有形式的一些其他领域,但他们没有任何的这种发挥作用。

There are some other fields in the form, but they don't play a part in any of this.

下面是Jquery的:

Here's the Jquery:

<script type="text/javascript">

    var artist_names = [<%= @artist_names %>];
    var venue_names = [<%= @venue_names %>];

    $(document).ready(function() {

        //Set button disabled
        $("input[type=submit]").attr("disabled", "disabled");

        var submitForm = function(date, headliner, venue) {

            //If form is validated enable form

            if ((date != "") && ($.inArray(headliner, artist_names) != -1) && ($.inArray(venue, venue_names) != -1)) 
                {
                    $("input:submit#event").removeAttr("disabled");
                }
                else
                {
                    $("input:submit#event").attr("disabled", true);
                }

            $('#typing').html(date+headliner + venue);
        }

        $("#datepicker").datepicker({
            onSelect: function(dateText, inst) { submitForm(this.value, $('#headliner').val(), $('#venue').val()) }
        });

        $("#headliner").autocomplete({
            source: artist_names,
            select: function(event, ui) { 
                submitForm($('#datepicker').val(), ui.item.value, $('#venue').val()); 
                $('#add-artist').empty();
                }
        });

        $("#venue").autocomplete({
            source: venue_names,
            select: function(event, ui) { 
                submitForm($('#datepicker').val(), $('#headliner').val(), ui.item.value); 
                $('#add-venue').empty(); 
                }
        });

        //Append a change event listener to you inputs
        $('#headliner').keyup(function() {
            var val = $('#headliner').val();
            if ($.inArray(val, artist_names) == -1) 
            {   
                if (val=="") 
                {
                    $('#add-artist').empty();
                }
                else 
                {
                    $('#add-artist').html("<strong>" + val + "</strong> isn't in our database. you must first add a profile for them to add this event!<br /><form action='/add-artist-event' method='post'><br />Artist Name: <input type='text' maxlength='50' name='artist_name' value='" + val +"' /><br /> Website: <input type='text' maxlength='50' name='artist_website' /><br /> Facebook: <input type='text' maxlength='50' name='artist_facebook' /><br />Twitter: <input type='text' maxlength='50' name='artist_twitter' /><br />Soundcloud: <input type='text' maxlength='50' name='artist_soundcloud' /><br />Bandcamp: <input type='text' maxlength='50' name='artist_bandcamp' /><br /><button type='button' id='add-artist-button' onclick='addNewArtist();'>Add New Artist!</button></form><br /><br />" );
                }
                $("input:submit#event").attr("disabled", true);
            }
            else
            {
                $('#add-artist').empty();
                submitForm($('#datepicker').val(), $('#headliner').val(), $('#venue').val()); 
            }

        });

        $('#venue').keyup(function() {
            var val = $('#venue').val();
            if ($.inArray(val, venue_names) == -1) 
            {   
                if (val=="") 
                {
                    $('#add-venue').empty();
                }
                else 
                {
                    $('#add-venue').html("<strong>" + val + "</strong>" + " is not in our database. you must first add a profile for this venue to add this event!<br /><form action='/add-venue-event' method='post'><br />Venue Name: <input type='text' maxlength='50' name='venue_name' value='" + val +"'/><br />Address: <input type='text' maxlength='50' name='venue_address' /><br />City: <input type='text' maxlength='50' name='venue_city' /><br />State: <input type='text' maxlength='50' name='venue_state' /><br />Zip: <input type='text' maxlength='50' name='venue_zip' /><br />Phone: <input type='text' maxlength='50' name='venue_phone' /><br /> Website: <input type='text' maxlength='50' name='venue_website' /><br /> Facebook: <input type='text' maxlength='50' name='venue_facebook' /><br />Twitter: <input type='text' maxlength='50' name='venue_twitter' /><br /><button type='button' id='add-venue-button' onclick='addNewVenue();'>Add New Venue!</button> </form><br /><br />");
                }
                $("input:submit#event").attr("disabled", true);
            }
            else
            {
                $('#add-venue').empty();
                submitForm($('#datepicker').val(), $('#headliner').val(), $('#venue').val()); 
            }

        });


    });

    var addNewArtist = function() { 
            $.post('/add-event/new-artist', {

                artist_name: $(':input[name="artist_name"]').val(),
                website: $(':input[name="artist_website"]').val(),
                facebook: $(':input[name="artist_facebook"]').val(),
                twitter: $(':input[name="artist_twitter"]').val(),
                soundcloud: $(':input[name="artist_soundcloud"]').val(),
                bandcamp: $(':input[name="artist_bandcamp"]').val()

            },

            function(output) {

                artist_names = output.split(',');

                $('#add-artist').html(output);
                $('#headliner').autocomplete("option", { source: artist_names });
            });
        };

    var addNewVenue = function() { 
            $.post('/add-event/new-venue', {

                venue_name: $(':input[name="venue_name"]').val(),
                street_address: $(':input[name="venue_address"]').val(),
                city: $(':input[name="venue_city"]').val(),
                state: $(':input[name="venue_state"]').val(),
                zip: $(':input[name="venue_zip"]').val(),
                phone: $(':input[name="venue_phone"]').val(),
                email: $(':input[name="venue_email"]').val(),
                website: $(':input[name="venue_website"]').val(),
                facebook: $(':input[name="venue_facebook"]').val(),
                twitter: $(':input[name="venue_twitter"]').val()

            },

            function(output) { 

                venue_names = output.split(',');

                $('#add-venue').html(output);
                $('#venue').autocomplete("option", { source: venue_names });
            });
    };
</script>

我希望避免,包括问题的所有的JavaScript,但我认为这是最好的,我做的。唯一的另外一块是,当我铬查看源文件我看到这两个变量确实得到填充数据:

I was hoping to avoid including all the javascript in the question, but I think it's best that I do. The only other piece is that when I "view source" in chrome I see the two variables are indeed getting populated with data:

var artist_names = [["'Bubonic Bear', "]];
var venue_names = [["'Electric Factory', "]];


更新

如果我硬code列表的风云组合字段的自动完成功能,自动完成的作品。


UPDATE

If I hardcode a list for the "headliner" field's autocomplete function, the autocomplete works.

$("#headliner").autocomplete({
            //source: artist_names,
            source: ["band1", "band2", "band3"],
            select: function(event, ui) { 
                submitForm($('#datepicker').val(), ui.item.value, $('#venue').val()); 
                $('#add-artist').empty();
                }
        });

我仍不能确定的解决方案,它似乎很奇怪,一个变量是在应用程序的本地实例访问,而不是在Heroku上。无论如何,我会添加更多,如果我搞清楚了。

I'm still not sure of the solution, it seems odd that a variable would be accessible in a local instance of the app and not on heroku. Anyway, I will add more if I figure out more.

推荐答案

我不明白如何将任何地方工作。数据源:

I don't see how that would work anywhere. Your data sources:

var artist_names = [<%= @artist_names %>];
var venue_names  = [<%= @venue_names %>];

出来的:

var artist_names = [["'Bubonic Bear', "]];
var venue_names  = [["'Electric Factory', "]];

这不是对jQuery的-UI自动完成插件的正确的格式。在选项应该是当你使用内联源字符串的简单数组。

and that's not the right format for the jQuery-UI Autocomplete widget. The source option should be a simple array of strings when you're using an inlined source.

您应该内联的阵列JSON格式:

You should be inlining your arrays in JSON format:

var artist_names = <%= @artist_names.to_json %>;
var venue_names  = <%= @venue_names.to_json  %>;

和应该产生这样的事情:

and that should produce things like this:

var artist_names = ["Bubonic Bear"];
var venue_names  = ["Electric Factory"];

和自动完成小部件会高兴。

and the autocomplete widget will be happy with that.

您可以使用此演示玩玩看的区别:

You can play with this demo to see the difference:

http://jsfiddle.net/ambiguous/Brb9p/

这篇关于jQuery的的UI自动完成本地工作不是在Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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