此代码正常工作,但当我点击提交按钮时,焦点反过来第一次专注于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();
}
});
});
答案 0 :(得分:1)
问题是,你处理具有这种结构的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();
}
});
});