通过JQ判断input当前输入法是中文还是英文模式

前端开发   发布日期:2023年05月28日   浏览次数:231

项目中要实现扫码枪扫描带字母的条型码,但如果当前input焦点处是中文输入法就无法触发回车事件,因为中文输入法是输入一个整体才会键入内容,所以就不能监听到结束再执行动作了。所以需要给用户提示,及时切换输入法。

通过JQ判断input当前输入法是中文还是英文模式:

<input name="zc_code" id="zc_code" type="text" value="" class="zc_code" maxlength="15"/>

<script>
function get_shurufa(){
    $('#zc_code').on('input', function() {
        if ($(this).prop('comStart')) return;  //中文输入过程中不截断
        //console.log('当前输入:' + $(this).val());
    }).on('compositionstart', function(){
        $(this).prop('comStart', true);
        //console.log('中文输入:开始->' + $(this).val());
        
        $("#tishi_str").html('请将输入法切换为“英文”模式!')
        $("#tishi").css("background","#FF0000");
        $("#tishi").fadeIn().fadeOut().fadeIn()
        $("#zc_code").val('')
        return false;
    }).on('compositionend', function(){
        $(this).prop('comStart', false);
        //console.log('中文输入:结束->'  + $(this).val());
        
        $("#tishi_str").html('请将输入法切换为“英文”模式!')
        $("#tishi").css("background","#FF0000");
        $("#tishi").fadeIn().fadeOut().fadeIn()
        $("#zc_code").val('')
        return false;
    });
}

$(document).ready(function(){
    get_shurufa()
});
</script>


以上就是通过JQ判断input当前输入法是中文还是英文模式的详细内容,更多关于通过JQ判断input当前输入法是中文还是英文模式的资料请关注九品源码其它相关文章!