尝试使用 tagify 插件用动态标签填充文本字段

发布于 2024-12-03 13:40:40 字数 2000 浏览 1 评论 0原文

有以下代码(驻留在对话框中)并充当事件侦听器 用于复选框。能够使用复选框中的值填充/取消填充文本字段 通过选中或取消选中各个复选框:

// 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< /a> 插件仅在文本字段内创建一个“标记动态标签”,但当我单击其他复选框时不会创建另一个标签。另外,当我取消选中原来的复选框时,它 不会删除原始标签。

这是我使用 JQuery tagify 插件的代码(我所做的就是保持所有内容与上面相同,但调用 tagify 函数 在文本字段上):

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

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

我在浏览器中收到此 JavaScript 错误:

jquery.tagify.js: 'options' is undefined, line 71

在源代码中,此行内容为:

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

它声明 options.key 未定义...

查看jquery.tagify.js完整源码请点击这里

我可能做错了什么?有没有办法我可以创建一个“其他”,例如:

// 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
});

如果有人可以帮助我,我将非常感激......

Have the following code which (resides in a dialog box) and serves as an event listener
for check boxes. Am able to populate / de-populate text field with values from check box
by checking or unchecking the individual check boxes:

// 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;
});

However, when I use try to use the JQuery Tagify plug-in it only creates one "tagified dynamic label" inside the text field but doesn't create another label when I click on an additional check box. Also, when I uncheck the original checkbox, it
doesn't remove the original label.

Here's my code using the JQuery tagify plug-in (all I did was keep everything the same as above but called the tagify function
on the text field):

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

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

I get this JavaScript error in the browser:

jquery.tagify.js: 'options' is undefined, line 71

In the source code this line reads:

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

Its stating that options.key is undefined...

To view the jquery.tagify.js complete source, please click here.

What am I possibly doing wrong? Is there a way I can create an "else" for example:

// 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
});

Would really appreciate it if someone could help me with this...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2024-12-10 13:40:40

使用以下代码可以使其正常工作:

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);
}

Got it working with the following code:

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文