html - jQuery 每个空的必填字段焦点和滚动顶部
问题描述
当需要一个字段并且字段值为0时,我想专注于该字段。我想通过从顶部滚动来在屏幕中间显示该字段。使用下面的代码滚动无法正常工作....
$('form.cartec').on('submit', function () {
if ($("input[required]").length > 0){
$('html , body').animate({
scrollTop: $("input[required]").focus().offset().top-30
}, 2000);
$("input[required]").focus();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form class="cartec" action="/action_page.php">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required>
<label for="password">Password:</label>
<input type="text" id="password" name="password" required>
<label for="location">Location:</label>
<input type="text" id="location" name="location" required>
<label for="brandcolor">Brand Color:</label>
<input type="text" id="brandcolor" name="brandcolor" required>
<label for="product-type">Product Type:</label>
<input type="text" id="product-type" name="product-type" required>
<input type="submit">
</form>
解决方案
推荐阅读
- reactjs - 如何在 Firebase 中只显示一个特定用户而不是全部?
- python - 更改数据框中的特定单元格 - 复制错误
- python - 在新的 DataFrame() 中更快地重新组织我的数据
- android - 如何在静音选项上修复通知 setSound
- html - 浏览器大小更改时图像无法与文本正确对齐 - 有时会从屏幕上消失
- python - 有没有一种方法可以将整个 jupyter 笔记本的输出保存到 txt 文件或类似文件中,而不使用命令行,而是从笔记本内部?
- selenium - 如何在运行中从 Perfecto 中选择可用设备
- angular - 创建 observable 以返回 csv 值
- rest - 如何使用 softlayer Rest API 在 IBM 云中使用数据磁盘启动计算实例
- installation - Sas Add-in for Microsoft office, installation error in office 365 / windows10 : Culture is not supported