如何检查焦点上的每个输入

How to check each input on focus

此代码有效,但当我首先单击提交按钮时,焦点发生反转,焦点在 textarea 框上未聚焦到在第一个输入上反转焦点。我该如何解决?

           <input type="text" class="textbox texrr" name="" />             
           <input type="text" class="textbox texrr" name="" />
           <textarea class="textbox1 texrr"></textarea>
           <input type="submit" class="send" value="Send" name="submit">


    $(".send").click(function(){
        $(".texrr").each(function(index, element) {
            if($(this).val()!=""){
                $(this).removeClass('texerrbg');
                } else{
                    $(this).addClass('texerrbg').focus();
                }
        });
    });

问题是,您处理的 jquery 数组具有以下结构:

['input.texrr', 'input.texrr', 'textarea.texrr']

并且当您使用 $.each 方法遍历它时,它会将焦点留在集合中没有任何数据的最后一个输入上。所以需要向后遍历这个数组

试试这个:

$(".send").click(function(){
        $($(".texrr").get().reverse()).each(function(index, element) {
            if($(this).val()!=""){
                $(this).removeClass('texerrbg');
            } else{
             $(this).addClass('texerrbg').focus();
            }
        });
    });