简体   繁体   English

jQuery从输入框中删除部分值

[英]jQuery Remove part of a value from an Input box

If you see my JSFiddle (bottom of post), my main problem occurs when I delete the tags from the input box on the left, they aren't removed from the box on the right, and I'm not sure how to go about it. 如果您看到我的JSFiddle(帖子的底部),则当我从左侧的输入框中删除标签时,我的主要问题就出现了,它们没有从右侧的框中删除,并且我不确定该怎么做它。

Basically I need to remove the part of a value from an input box that is present after the second to last comma. 基本上,我需要从倒数第二个逗号之后出现的输入框中删除值的一部分。

My problem in more detail: 我的问题更详细:

I am creating a tag form in the style of stack overflow. 我正在按照堆栈溢出的样式创建标记表单。

At the moment I have 2 input boxes. 目前,我有2个输入框。 The one on the left is the one users will use. 左边的一个是用户将使用的一个。 When they add tags to this box, the tags are styled in span tags so it looks user friendly. 当他们将标签添加到此框时,标签将以span标签样式化,因此看起来用户友好。

At the same time, the tags are also added to the input box on the right (which i will have hidden). 同时,标签也被添加到右侧的输入框中(我将隐藏它)。 The box on the right must have each tag separated by commas, as this is the format necessary for when the form gets submitted. 右边的框必须用逗号分隔每个标签,因为这是提交表单时必需的格式。

I have got so far, but the problem is when I delete the tags from the first box, they aren't removed from the box on the right..I'm unsure how to do it. 到现在为止,但是问题是,当我从第一个框删除标签时,它们并未从右侧的框中删除。.我不确定该怎么做。 Maybe I will have to match the strings? 也许我必须匹配字符串?

Code: 码:

Html: HTML:

<span id="tags"></span><input id="enterTag" type="text" />

<input id="hiddenbox" type="text"/>

JS: JS:

$('#enterTag').keydown(function(event) {
    var tag = $(this).val();
    // 188 is comma, 13 is enter
    if (event.which === 188 || event.which === 13) {
        if (tag) {
            $('#tags').append('<span>' + tag + '</span>');
            $(this).val('');
           $('#hiddenbox').val($('#hiddenbox').val() + tag+', ');
        }
        event.preventDefault();
    }
    // 8 is backspace
    else if (event.which == 8) {
        if (!tag) {
            $('#tags span:last').remove();
            event.preventDefault();
        }
    }
});​

JSFiddle: http://jsfiddle.net/greylien/pUfNu/9/ JSFiddle: http : //jsfiddle.net/greylien/pUfNu/9/

try 尝试

$('#enterTag').keydown(function(event) {
    var tag = $(this).val();
    // 188 is comma, 13 is enter
    if (event.which === 188 || event.which === 13) {
        if (tag) {
            $('#tags').append('<span>' + tag + '</span>');
            $(this).val('');
           $('#hiddenbox').val($('#hiddenbox').val() + tag+', ');
        }
        event.preventDefault();
    }
    // 8 is backspace
    else if (event.which == 8) {
        if (!tag) {
            $('#tags span:last').remove();
            var str = $('#hiddenbox').val();
            var str_a = str.split(',');
            str_a.pop();
            str_a.pop();
            if(str_a.length>0){
                var str_val = str_a.join(',')+', ';
            }else{
                var str_val = '';
            }
            $('#hiddenbox').val(str_val );
            event.preventDefault();
        }
    }
});​

js fidle : js fidle:

http://jsfiddle.net/pUfNu/11/ http://jsfiddle.net/pUfNu/11/

You can do this using a regular expression and simply remove the last segment from the string: 您可以使用正则表达式执行此操作,只需从字符串中删除最后一个段:

else if (event.which == 8) {
    if (!tag) {
        $('#tags span:last').remove();
        $("#hiddenbox").val($("#hiddenbox").val().replace(/,[^,]+,\s$/, ", "));
        event.preventDefault();
    }
}
$('#enterTag').keydown(function(event) {
    var tag = $(this).val();
    // 188 is comma, 13 is enter
    if (event.which === 188 || event.which === 13) {
        if (tag) {
            $('#tags').append('<span>' + tag + '</span>');
            $(this).val('');
            $('#hiddenbox').val($('#hiddenbox').val() + tag+', ');
        }
        event.preventDefault();
    }
    // 8 is backspace
    else if (event.which == 8) {
        if (!tag) {
            $('#tags span:last').remove();
            event.preventDefault();
            var s = $('#hiddenbox').val();
            l = s.lastIndexOf(',');
            s=s.substr(0,l);
            l = s.lastIndexOf(',');
            s=s.substr(0,l);
            $('#hiddenbox').val(s);
        }
    }
});​

http://jsfiddle.net/alexgeorgiou/TdTZV/1/ http://jsfiddle.net/alexgeorgiou/TdTZV/1/

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

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