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

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

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

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

  1. <input name="zc_code" id="zc_code" type="text" value="" class="zc_code" maxlength="15"/>
  2.  
  3. <script>
  4. function get_shurufa(){
  5. $('#zc_code').on('input', function() {
  6. if ($(this).prop('comStart')) return; //中文输入过程中不截断
  7. //console.log('当前输入:' + $(this).val());
  8. }).on('compositionstart', function(){
  9. $(this).prop('comStart', true);
  10. //console.log('中文输入:开始->' + $(this).val());
  11. $("#tishi_str").html('请将输入法切换为“英文”模式!')
  12. $("#tishi").css("background","#FF0000");
  13. $("#tishi").fadeIn().fadeOut().fadeIn()
  14. $("#zc_code").val('')
  15. return false;
  16. }).on('compositionend', function(){
  17. $(this).prop('comStart', false);
  18. //console.log('中文输入:结束->' + $(this).val());
  19. $("#tishi_str").html('请将输入法切换为“英文”模式!')
  20. $("#tishi").css("background","#FF0000");
  21. $("#tishi").fadeIn().fadeOut().fadeIn()
  22. $("#zc_code").val('')
  23. return false;
  24. });
  25. }
  26.  
  27. $(document).ready(function(){
  28. get_shurufa()
  29. });
  30. </script>


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