<input>中中文输入时input事件处理

来源:互联网 发布:php 发送模板消息代码 编辑:程序博客网 时间:2024/05/16 01:14

在input标签中进行中文输入时,输入拼音状态下尚未确认汉字输入时,input事件也在不断的被触发,如下图chengxuyuan

如果在input事件中对文字长度进行截断时,就会发生无法继续输入的问题,一种解决方法如下:

$(element).on('input', function() {
                    if($(this).prop('comStart')) return;    // 确保中文拼音输入过程中不被截断


                    that.onInputChange();
                }).on('compositionstart', function(){
                    $(this).prop('comStart', true);
                }).on('compositionend', function(){
                    $(this).prop('comStart', false);
                });

0 0