简体   繁体   English

使用按钮将标签添加到输入字段

[英]Add tags to input field with buttons

I'm using a plugin for Wordpress called WP User Frontend. 我正在为Wordpress使用名为WP User Frontend的插件。 Basically it allows users to post from the front-end of the website. 基本上,它允许用户从网站的前端发布。

There is a feature which allows users to add tags themselves, however, this is just a normal input text field, and I don't want them to create countless of new tags. 有一个功能允许用户自己添加标签,但是,这只是普通的输入文本字段,我不希望他们创建无数的新标签。

So basically what I want is for them to be able to click on several buttons which will add the tags to the input field, while also disabling their manual input. 因此,基本上,我希望他们能够单击几个按钮,这会将标签添加到输入字段,同时也禁用他们的手动输入。

You will need a script that converts the checkbox input into whatever format (say, a string of words separated by spaces) was needed for the original tags input. 您将需要一个脚本,用于将复选框输入转换为原始标签输入所需的任何格式(例如,用空格分隔的单词字符串)。 Here is an example script that would both insert the checkboxes to the page underneath the original input and put the resulting values into the original field, ensuring it gets posted the same way. 这是一个示例脚本,该脚本既可以将复选框插入到原始输入下方的页面上,又可以将结果值放入原始字段中,以确保以相同的方式发布。 (You could then hide the original field if you don't want it displayed.) http://jsfiddle.net/zLVTp/8/ (如果不想显示原始字段,则可以将其隐藏。) http://jsfiddle.net/zLVTp/8/

var tags = ['cows', 'sheep', 'dogs'],
    inp, label, ch = document.createElement('fieldset'),
    f = document.getElementById('tagField'); 
        // ^ The original input has id=tagField
f.readOnly = true;
for (var i = 0; i < tags.length; i++) {
    inp = document.createElement('input');
    label = document.createElement('label');
    label.innerHTML = tags[i];
    label.htmlFor = 'tag_' + tags[i];
    inp.type = "checkbox";
    inp.name = "tagSet";
    inp.className = 'tagCBs';
    inp.value = tags[i];
    inp.id = 'tag_' + tags[i];
    ch.appendChild(inp);
    ch.appendChild(label);
    inp.onchange = (function() {
        var th = inp;
        return function() {
            var sp, r = [];
            if (th.checked && f.value.indexOf(th.value) == -1) {
                if (f.value == '') f.value = th.value;
                else f.value += ' ' + th.value;
            }
            else if (!th.checked) {
                sp = f.value.split(' ');
                for (var j = 0; j < sp.length; j++) {
                    if (sp[j] != th.value) r.push(sp[j]);
                }
                f.value = r.join(' ');
            }
        }
    })();
}
f.parentNode.appendChild(ch);​

html checkbox seems to fit your need, no ? html复选框似乎符合您的需求,不是吗? you have to modify the form and replace the text-area with checkbox (dirty but simple) 您必须修改表单并将文本区域替换为复选框(肮脏但简单)

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

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