2012-08-01 85 views
0

嗨在這裏我有一個數組變量鏈接。我必須動態添加keywordslabel的值。你如何添加這個?謝謝在數組中添加一個動態變量到一個jQuery函數

更新

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> 
+0

我需要爲關鍵字和標籤動態添加值 – JavaH 2012-08-01 12:49:29

+0

您如何準確確定要將值添加到哪個關鍵字數組? – AdityaParab 2012-08-01 12:50:24

+0

我在關鍵字和標籤的數據庫中有一些價值。是否可以添加值? – JavaH 2012-08-01 12:53:09

回答

1

常規方式,

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

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

讓我們知道你如何確定index值,這樣我們就可以以更具體的方式幫助你:)

+0

謝謝你的回覆...索引從零開始,鏈接[index] .keywords [links [index] .keywords.length] =「你的新值」;我不明白這一行 – JavaH 2012-08-01 12:57:19

+0

它就像'links [index] .keywords'將選擇關鍵字數組...'links [index] .keywords [links [index]。關鍵字.length]'會給你選定數組的長度。如果該關鍵字數組在最後添加新元素... – AdityaParab 2012-08-02 07:41:46

2

添加新標籤

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

添加關鍵字,則需要通過鏈接陣列

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

上面的代碼正在考慮要添加到「空標籤」的關鍵字進行迭代標籤

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

相關問題