2011-09-07 39 views
0

請使用以下代碼(駐留在對話框中)作爲複選框的事件偵聽器 。我能夠填充/去填充勾選或取消勾選各個複選框從複選框 值文本字段:嘗試使用tagify插件來使用動態標籤填充文本字段

// Event listener which picks individual contacts 
// and populates input field. 
$('#emailCheckListId_ul input:checkbox').change(function() { 
     // Declare array 
     var emails = []; 

     // Iterate through each array and put email addresses into array 
     $('#emailCheckListId_ul input:checkbox:checked').each(function() { 
      emails.push($(this).val()); 
     }); 

     // Assign variable as To: text field by obtaining element's id. 
     var textField = document.getElementById("root.emailTextField"); 

     // Add/Remove array from text field 
     textField.value = emails; 
}); 

然而,當我使用嘗試使用JQuery Tagify插件只創建一個「標籤化的動態標籤「,但在我點擊其他複選框時不會創建另一個標籤。另外,當我取消選中原始複選框時, 不會刪除原始標籤。

下面是使用jQuery tagify插件(我所做的只是把一切都與上面相同,但呼籲文本字段tagify功能 )我的代碼:

// Add/Remove array from text field 
textField.value = emails; 

// Decorate with dynamic label 
$(textField).tagify(emails); 

我得到這個JavaScript錯誤瀏覽器:

jquery.tagify.js: '選項' 是未定義的,線71

在源代碼中該行的內容:

_setOption: function(key, value) { 
    options.key = value; 
}, 

它指出options.key未定義...

要查看jquery.tagify.js完整的源代碼,請點擊here

我可能做錯了什麼?有沒有一種方法,我可以創造一個「其他」,例如:

// Iterate through each array and put email addresses into array 
$('#emailCheckListId_ul input:checkbox:checked').each(function() 
{ 
    // do something 
}); 

// how would I define the else when the input:checkbox:checked is not checked: 

// I already tried this but it doesn't work: 
$('#emailCheckListId_ul input:checkbox:unchecked').each(function() 
{ 
    // do something else 
}); 

會很感激,如果有人可以幫助我這個...

感謝您抽出時間來閱讀。

回答

0

得到它用下面的代碼工作:

if (emails.length <= 0) { 
    // Remove dynamic labels 
    $(textField).tagify('destroy'); 

    // Empty text field 
    textField.value = ""; 
} 
else { 
    // Reset tagify 
    $(textField).tagify('destroy'); 

    // Add/Remove array from text field 
    textField.value = emails; 

    // Decorate with dynamic label 
    $(textField).tagify(emails); 
} 
相關問題