Jquery判断输入法输入和非输入法输入

267 阅读1分钟

input输入框判断:

 $('input').on('input', function() {
        if ($(this).prop('comStart')) return;  //中文输入过程中不截断
        console.log('当前输入:' + $(this).val());
}).on('compositionstart', function(){
        $(this).prop('comStart', true);
        console.log('中文输入:开始->' + $(this).val());
}).on('compositionend', function(){
        $(this).prop('comStart', false);
        console.log('中文输入:结束->'  + $(this).val());
});


select输入框判断:

$('#select_id' .layui-select-title input').bind('input propertychange', function() {
     //这里给function一个事件参数命名为e,叫event也行,随意的,e就是IE窗口发生的事件
         if ($(this).prop('comStart')) return;  //中文输入过程中不截断
          console.log('当前输入:' + $(this).val());
}).on('compositionstart', function(){
        $(this).prop('comStart', true);
        console.log('中文输入:开始->' + $(this).val());
}) .on('compositionend', function(){
        $(this).prop('comStart', false);
        console.log('中文输入:结束->'  + $(this).val());
});

input输入框和select下拉框判断时大致一样,只是select下拉框需要添加一个事件。

页面加载时就调用,需要写在$(document).ready(function(){ ...do something});