value replace会在输入结尾错误地添加值

时间:2012-03-29 06:27:47

标签: javascript jquery

我正在尝试在输入字段中执行值替换。它几乎正常工作。问题是当我只想将它添加到一个位置时,它会将值添加到输入的末尾

HTML:

<input id="boom" value="03/15/0212" />​

Jquery:我已经硬编码了一些值以防止放置所有函数

$('#boom').on('keypress', function (e)
{
    var crs = this.selectionStart;
    var from;
    var to;
    var sub;
    _dateSection = 'month';
    var currentObj = this;
    var currentVal = $(this).val();
    var c = String.fromCharCode(e.which);
    var stringSplit = currentVal.split('/');
    switch (_dateSection) {
        case 'month':
            _currentDateValue = stringSplit[0];
            from = 0;
            to = 2;
            sub = currentVal.substring(from, to);
            break;
        case 'day':
            _currentDateValue = stringSplit[1];
            from = 3;
            to = 6;
            sub = currentVal.substring(from, to);
            break;
        case 'year':
            _currentDateValue = stringSplit[2];
            from = 6;
            to = 10;
            sub = currentVal.substring(from, to);
            break;
    };
    var check = false;
    if (!check) {
        $(currentObj).val(function (index, value)
        {
            return value.replace(sub, c);
        });
    }
});

JsFiddle

如果在当前填充03的输入的月份部分中添加3,则替换正确为3,但它也会在年末添加3。年度,2012年,年份值应保持2012年。

最终值应为

3/15/2012

不是

3/15/20123

修改

此代码提出了同样的问题:

$(currentObj).val(currentVal.replace(sub, c));

就像这样

var replaceValue = currentVal.replace(sub, c)
$(currentObj).val(replaceValue);

2 个答案:

答案 0 :(得分:1)

添加到末尾的额外字符由输入字段本身添加。按键事件也由输入处理,它执行正常操作并将字符添加到值中。

只需将e.preventDefault();放入事件处理程序中,以防止输入事件发生。


另外,不要使用replace将字符放在字符串中,使用substr来获取要保留的字符串部分:

value = value.substr(0, from) + c + value.substr(to, c);

如果该字符串例如是05/05/2012,并且您尝试将该日替换为4,那么它将替换第一个例子而您将获得4/05/2012而不是05/4/2012

答案 1 :(得分:0)

这对我有用: -

$('#boom').on('keyup', function (e)
            {
                var crs = this.selectionStart;
                var from;
                var to;
                var sub;
                _dateSection = 'month';
                var currentObj = this;
                var currentVal = $(this).val();
                var c = String.fromCharCode(e.which);
                var stringSplit = currentVal.split('/');
                switch (_dateSection) {
                    case 'month':
                          _currentDateValue = stringSplit[0];
                          sub = _currentDateValue;
                         c=Number(sub)+Number(c);
                         break;
                    case 'day':
                        _currentDateValue = stringSplit[1];
                        from = 3;
                        to = 6;
                        sub = currentVal.substring(from, to);
                        break;
                    case 'year':
                        _currentDateValue = stringSplit[2];
                        from = 6;
                        to = 10;
                        sub = currentVal.substring(from, to);
                        break;
                };
                var check = false;
                if (!check) {
                    $(currentObj).val(function (index, value)
                    {

                        value=value.substring(from,value.length-1);
                        return value.replace(sub, c);
                    });
                }
        });