首页 > 解决方案 > 通过使用 jquery 引用类名来关注下一个输入元素

问题描述

当用户在文本框中输入值时,如果该输入字段包含特定的类名,则应移至下一个文本框,但如果另一个文本框没有该类名,则不应移至下一个文本框

<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="txt_cls" />
<input type="text" maxlength="1" class="" />
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>

$(document).ready(function(){
    $('input.txt_cls').on("input", function(){
        if($(this).val().length==$(this).attr("maxlength")){

            // $(this).next().focus();

        $(this).next().find('.txt_cls').focus();
        }
    });
});
</script>

标签: javascriptjquery

解决方案


哦,我犯了一个小错误,忘了提 .closest(); 现在它工作正常

$(document).ready(function(){
    $('input.txt_cls').on("input", function(){
        if($(this).val().length==$(this).attr("maxlength")){
        //$(this).next().focus();
         $(this).next().closest('.txt_cls').focus();
        }
    });
});

推荐阅读