将数组中的动态变量添加到jQuery函数中

时间:2012-08-01 12:45:42

标签: jquery arrays variables

大家好,我有一个数组变量链接。我必须动态添加keywordslabel的值。你怎么加这个?谢谢

更新

MyIssue 我需要在该数组中动态添加(推送)关键字和标签的值以进行自动完成。怎么做?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Autocomplete - Default functionality</title>
    <link rel="stylesheet" href="http://jqueryui.com/themes/base/jquery.ui.all.css">
     <script type="text/javascript" charset="utf-8" src="../js/cordova-1.7.0.js"></script>
 <script type="text/javascript" charset="utf-8" src="../js/jquery-1.7.2.js"></script>

    <script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.position.js"></script>
    <script src="http://jqueryui.com/ui/jquery.ui.autocomplete.js"></script>
    <link rel="stylesheet" href="http://jqueryui.com/demos/demos.css">
    <script>
    $(function() {
        var links = [
        {
            keywords: ['create', 'add', 'make', 'insert', 'user'],
            label: "Create user",
            //desc: "Create a user in the system",
            //url: 'http://mysite.com/user/create/'
        },
        {
            keywords: ['create', 'add', 'make', 'insert', 'organisation'],
            label: "Create organisation",
           // desc: "Create an organisation in the system",
           // url: 'http://mysite.com/organisation/create/'
        }];
        $( "#tags" ).autocomplete({

             source: function(request, response) {  
             var matched = [];

        for (var k = 0; k < links.length; k++) {
            if (checkSearchWordsMatchKeywords(request.term, links[k]['keywords'])) {
                matched.push(links[k]);
            }
        }
        // display the filtered results
        response(matched);

             } 
        });

        function checkSearchWordsMatchKeywords(searchWords, keywords)
        {
            var searchWords = searchWords.toLowerCase();    // Lowercase the search words
            var searchWords = searchWords.split(' ');       // Break up the search into separate words
            var numOfSearchWords = searchWords.length;      // Count number of search words
            var numOfKeywords = keywords.length;            // Count the number of keywords
            var matches = [];                               // Will contain the keywords that matched the search words

            // For each search word look up the keywords array to see if the search word partially matches the keyword
            for (var i = 0; i < numOfSearchWords; i++)
            {
                // For each keyword
                for (var j = 0; j < numOfKeywords; j++)
                {   
                    // Check search word is part of a keyword
                    if (keywords[j].indexOf(searchWords[i]) != -1)
                    {
                        // Found match, store match, then look for next search word
                        matches.push(keywords[j]);
                        break;
                    }
                }
            }
            if (matches.length == numOfSearchWords)
            {

                return true;
            }
            }

    });
    </script>
</head>
<body>

<div class="demo">

<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" />
</div>

</div><!-- End demo -->

</body>
</html>

3 个答案:

答案 0 :(得分:2)

添加新标签

newLabel = { "keywords":[], "label":"Empty Label" };
links.push(newLabel);

要添加到关键字,您需要遍历链接数组

$(links).each(function(){ if(this["label"] = "Empty Label") { this["keywords"].push("newKeyword") } });    

以上代码正在考虑您要添加到“空标签”标签的关键字

答案 1 :(得分:1)

一般方法是(前提是您知道关键字数组的索引),

links[index].keywords[links[index].keywords.length] = "your new value";

links[index].label = "your new label value";

告诉我们如何确定index值,以便我们以更具体的方式为您提供帮助:)

答案 2 :(得分:0)

将键和标签设置为变量,然后将键添加到数组中标签的索引:

var links = new Array();
var keys = ['create', 'add', 'make', 'insert', 'user'];
var label = "Create user";

links[label] = keys;

alert(links["Create user"]);

提醒:create,add,make,insert,user