繁体   English   中英

从输入中删除字符串? jQuery的

[英]Remove string from input on click? jQuery

我正在尝试编写自己的函数,以将每个字符串用逗号分隔,将其包装在标签中,我可以使用此功能,但是在“关闭”单击ID上,例如想从输入中删除相应的字符串,这可能吗?

jQuery的

$('body').on('click', '.tag span', function(){ 
    $(this).parent().hide();
});
$('input').keyup(function(e) {
        $('.result').html('');
        var valueArr = $(this).val().split(',');
        for(var i=0; i<valueArr.length; i++){$('.result').append('<div class="tag">'+valueArr[i]+'<span> X</span></div>')};
});

http://jsfiddle.net/Liama​​tvenn/7huQ4/1/

尝试这个:

$('body').on('click', '.tag span', function(){ 
    $(this).parent().hide();
    $(this).closest('.result').prev('input').val(""); //get the input respective to the clicked close button.
});

.closest()会将您带到父.result并且仅定位使用.prev()生成标记的先前input

演示

更简单的方法是,使用clicked标签项目的索引,并根据与该标签本身插入顺序相同的索引,从数组中删除相应的项目。

$('body').on('click', '.tag span', function () {
    var $parent = $(this).closest('.tag');
    var index = $parent.index(); //Get the index of the `tag`

    $(this).closest('.result').prev('input').val(function (_, value) { //use val function argument
        var values = value.split(','); //split the value
        values.splice(index, 1); //remove the item from array
        return values.join(','); //join them back
    });

    $parent.remove(); //then remove your tag
});

演示

您可以随时修改点击处理程序以执行以下操作:

$('body').on('click', '.tag span', function(){
    var $this, $input, val, arr;
    $this = $(this);
    $input = $('input');
    arr = $input.val().split(',');
    val = $this.parent().text().replace(' X','');
    arr.splice(arr.indexOf(val), 1);
    $input.val(arr.join(',')); 
    $this.parent().hide();
});

您可以在http://jsfiddle.net/eF5ev/上看到它的运行情况

您可以只将元素存储在数组中,然后删除由其索引单击的元素:

var $tag = $('<div class="tag"><span class="text"></span><span class="close">&times;</span></div>');
var tags = [];

$('.result').on('click', '.tag .close', function() {
    var $parent = $(this).parent();

    tags.splice($parent.index(), 1);
    $parent.remove();

    $('input').val(tags.join(', '));
});

$('input').keyup(function() {
    tags = []
    $('.result').empty();

    $(this.value.split(/\s*,\s*/)).each(function(index, value) {
        var tag = $tag.clone();

        tag.find('.text').text(value);
        tag.appendTo('.result');

        tags.push(value);
    });
});

演示: http : //jsfiddle.net/7huQ4/9/

您可以在关闭按钮单击时输入数据。 jQuery text()方法获取标记内的文本。 从输入数据中删除该文本。 然后使用正则表达式删除前导和尾随逗号。

试试这个。

    $('input').keyup(function(e) {
                var input = $(this); /* Store input object for replaced value insertion */
                $('.result').html('');
                var valueArr = $(this).val().split(',');

                for (var i = 0; i < valueArr.length; i++) {
                    $('.result').append('<div class="tag">' + valueArr[i] + '<span> X</span></div>');
                }
                $('span').click(function() {
                    /* Removal of unwanted string like ' X' (close button) */
                    var data = $(this).parent().text();
                    data = data.replace($(this).text(), ""); /* Basic javascript replace() function*/
                    /* Delete unwanted commas, Store modified string to input field */
                    input.val(input.val().replace(data, '')
                                         .replace(/^[,\s]+|[,\s]+$/g, '')
                                         .replace(/,[,\s]*,/g, ','));
                });
            });

演示:http://jsfiddle.net/7uqEg/

暂无
暂无

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

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