简体   繁体   English

Javascript中的意外标识符

[英]Unexpected Identifier in Javascript

I am adding content to a list dynamically and I want to be able to prevent adding duplicates. 我正在动态地向列表添加内容,我希望能够防止添加重复项。 So before content is added I am selecting all the list, make an array and check that the data I am adding is not already contained in the list. 因此,在添加内容之前,我选择所有列表,创建一个数组并检查我添加的数据是否已包含在列表中。 However I get a unexpected identifier in my code. 但是我的代码中有一个意外的标识符。 This is my code: 这是我的代码:

$(".autocomp_centers").autocomplete({
    var ids = new Array();
    $("#sister-centers-list > li").each(function(index, value){
        ids.push($(value).attr('class'));
        console.log($(value).attr('class'));
    });

    serviceUrl:'/suggest_centers',
        maxHeight:400,
        width:252,
        params: {country: $("#country").val() },
        minChars:2,
        onSelect: function(value, data){
            if ($.inArray(data, ids) == -1){
                alert("Sister center has already been added.");
            }else{
                $("#hidden-ids").append('<input type="hidden" name="center_ids[]" value="' + data +'" class="center-' + data + '"/>');
                $('#sister-centers-list').append('<li class="center-' + data + '">' + value + ' <a href="#sister-center" class="remove-center-before-save" id="' + data + '">Remove</a></li>');
            }               
            $("#sister-search").val("");
        }
});

When the person starts typing and this method is called, I get the list values and store them in an array. 当这个人开始输入并调用这个方法时,我得到列表值并将它们存储在一个数组中。 When they select a value, I check if the value is already in the array. 当他们选择一个值时,我会检查该值是否已经在数组中。 This is the line that causes the error: var ids = new Array(); 这是导致错误的行: var ids = new Array();

You've started an object literal when it looks like you intended to start a function. 当你想要启动一个函数时,你已经启动了一个对象文字。

{
    var ids = new Array();

You probably want: 你可能想要:

function () {
    var ids = new Array();

The autocomplete command ( http://api.jqueryui.com/autocomplete/ ) requires an object as a parameter -- you cannot write the code directly between the object properties. 自动完成命令( http://api.jqueryui.com/autocomplete/ )需要一个对象作为参数 - 您无法直接在对象属性之间编写代码。

Try something like this instead: 尝试这样的事情:

var ids = new Array();
$("#sister-centers-list > li").each(function(index, value){
    ids.push($(value).attr('class'));
    console.log($(value).attr('class'));
});

$(".autocomp_centers").autocomplete({
    serviceUrl:'/suggest_centers',
    maxHeight:400,
    width:252,
    params: {country: $("#country").val() },
    minChars:2,
    onSelect: function(value, data){
        if ($.inArray(data, ids) == -1){
            alert("Sister center has already been added.");
        }else{
            $("#hidden-ids").append('<input type="hidden" name="center_ids[]" value="' + data +'" class="center-' + data + '"/>');
            $('#sister-centers-list').append('<li class="center-' + data + '">' + value + ' <a href="#sister-center" class="remove-center-before-save" id="' + data + '">Remove</a></li>');
        }               
        $("#sister-search").val("");
    }
});

Of course, depending on what your application does, you may have to make more changes. 当然,根据您的应用程序的功能,您可能需要进行更多更改。 For example if you have multiple "autocomplete" things on the same page, you may want to use a separate array of IDs for each of them. 例如,如果您在同一页面上有多个“自动完成”内容,则可能需要为每个内容使用单独的ID数组。 Etc. 等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM