javascript - 通过使用 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>
解决方案
哦,我犯了一个小错误,忘了提 .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();
}
});
});
推荐阅读
- php - htaccess 文件将 /stringa 重定向到 /index.php?action=stringb
- javascript - 如何使用 id 获取多个复选框值
- python - 如何在张量流中实现随机池化,在每个滑动窗口中获取随机像素
- python-3.x - 获取列表中的一组数字,其总和与给定的所需数字匹配
- python - 插值标准函数 python, func(10,16,2) -> [10 12 14 16]
- c# - 如何将变量放入c#属性中?
- javascript - 在 x 个字符后阅读更多内容 JavaScript
- notifications - 是否有任何应用程序可以从 Google 日历事件中创建(假)传入 SMS?
- c# - MVC 5 数据注释“不等于零”
- java - 在 CSV 行末尾查找未终止的引用字段