
本文旨在提供一个清晰的指南,帮助开发者在使用 jQuery 验证 Bootstrap 表格中的输入字段时,能够同时处理文本框、日期选择器、下拉菜单等多种类型的输入,确保所有字段在提交前都已填写,并提供视觉反馈。通过修改现有的 jQuery 代码,使其能够识别并验证所有类型的输入,并使用 CSS 类来高亮显示未填写的字段,从而提升用户体验和数据完整性。
问题分析
原有的 jQuery 代码只针对 input[type=”text”] 类型的输入框进行验证,导致日期选择器 (input[type=”date”]) 和下拉菜单 (select) 等其他类型的输入字段无法被正确验证。因此,需要修改选择器,使其能够包含所有需要验证的输入类型。
解决方案
修改 jQuery 代码中的选择器,使其能够匹配所有需要验证的输入类型。将 .find(‘input[type=”text”]’) 修改为 .find(‘input[type=”text”], input[type=”date”], select’), 即可同时选中文本输入框、日期选择器和下拉菜单。
修改后的代码片段:
// Add row on add button click$(document).on("click", ".add", function(){ var empty = false; var input = $(this).parents("tr").find('input[type="text"], input[type="date"], select'); input.each(function(){ if(!$(this).val()){ $(this).addClass("error"); empty = true; } else{ $(this).removeClass("error"); } }); $(this).parents("tr").find(".error").first().focus(); if(!empty){ input.each(function(){ $(this).parent("td").html($(this).val()); }); $(this).parents("tr").find(".add, .edit").toggle(); $(".add-new").removeAttr("disabled"); } });
完整示例代码:
以下是一个完整的 HTML 示例,展示了如何将修改后的 jQuery 代码应用到 Bootstrap 表格中,以验证文本输入框、日期选择器和下拉菜单的非空值。
Bootstrap Table with Add and Delete Row Feature body { color: #404E67; background: #F5F7FA; font-family: 'Open Sans', sans-serif; } .table-wrapper { width: 700px; margin: 30px auto; background: #fff; padding: 20px; box-shadow: 0 1px 1px rgba(0,0,0,.05); } .table-title { padding-bottom: 10px; margin: 0 0 10px; } .table-title h2 { margin: 6px 0 0; font-size: 22px; } .table-title .add-new { float: right; height: 30px; font-weight: bold; font-size: 12px; text-shadow: none; min-width: 100px; border-radius: 50px; line-height: 13px; } .table-title .add-new i { margin-right: 4px; } table.table { table-layout: fixed; } table.table tr th, table.table tr td { border-color: #e9e9e9; } table.table th i { font-size: 13px; margin: 0 5px; cursor: pointer; } table.table th:last-child { width: 100px; } table.table td a { cursor: pointer; display: inline-block; margin: 0 5px; min-width: 24px; } table.table td a.add { color: #27C46B; } table.table td a.edit { color: #FFC107; } table.table td a.delete { color: #E34724; } table.table td i { font-size: 19px; } table.table td a.add i { font-size: 24px; margin-right: -1px; position: relative; top: 3px; } table.table .form-control { height: 32px; line-height: 32px; box-shadow: none; border-radius: 2px; } table.table .form-control.error { border-color: #f50000; } table.table td .add { display: none; } $(document).ready(function(){ $('[data-toggle="tooltip"]').tooltip(); var actions = $("table td:last-child").html(); // Append table with add row form on add new button click $(".add-new").click(function(){ $(this).attr("disabled", "disabled"); var index = $("table tbody tr:last-child").index(); var row = '' + ' '; $("table").append(row); $("table tbody tr").eq(index + 1).find(".add, .edit").toggle(); $('[data-toggle="tooltip"]').tooltip(); }); // Add row on add button click $(document).on("click", ".add", function(){ var empty = false; var input = $(this).parents("tr").find('input[type="text"], input[type="date"], select'); input.each(function(){ if(!$(this).val()){ $(this).addClass("error"); empty = true; } else{ $(this).removeClass("error"); } }); $(this).parents("tr").find(".error").first().focus(); if(!empty){ input.each(function(){ $(this).parent("td").html($(this).val()); }); $(this).parents("tr").find(".add, .edit").toggle(); $(".add-new").removeAttr("disabled"); } }); // Edit row on edit button click $(document).on("click", ".edit", function(){ $(this).parents("tr").find("td:not(:last-child)").each(function(){ var inputType = 'text'; if ($(this).find('input[type="date"]').length) { inputType = 'date'; } else if ($(this).find('select').length) { inputType = 'select'; } var currentValue = $(this).text(); var inputHtml = ''; if (inputType === 'text' || inputType === 'date') { inputHtml = ''; } else if (inputType === 'select') { var optionsHtml = 'ActiveInactive'; inputHtml = '' + optionsHtml + ''; } $(this).html(inputHtml); }); $(this).parents("tr").find(".add, .edit").toggle(); $(".add-new").attr("disabled", "disabled"); }); // Delete row on delete button click $(document).on("click", ".delete", function(){ $(this).parents("tr").remove(); $(".add-new").removeAttr("disabled"); }); });' + ' ' + ' ' + ' ActiveInactive ' + '' + actions + ' ' + '
注意事项
选择器范围: 根据实际需求,可能需要调整选择器以包含其他类型的输入字段,例如 textarea 等。验证逻辑: .val() 方法适用于大多数输入类型,但对于某些特殊类型的输入,可能需要使用其他方法来获取其值。用户体验: 除了高亮显示未填写的字段外,还可以添加其他用户友好的提示信息,例如在字段旁边显示错误消息。
总结
通过修改 jQuery 代码中的选择器,可以轻松地扩展验证范围,使其能够处理多种类型的输入字段。这有助于确保所有必需的字段在提交前都已填写,从而提高数据的完整性和用户体验。记住,根据实际需求调整选择器和验证逻辑,并提供清晰的错误提示,对于构建健壮且用户友好的表单至关重要。
以上就是使用 jQuery 验证 Bootstrap 表格中所有输入类型的非空值的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1575641.html
微信扫一扫
支付宝扫一扫