html - 显示内嵌图像的单选按钮的验证消息
问题描述
所以我有一个 div,其中有三个单选按钮,图像全部内联,我想在没有用户输入时使用引导程序在单击提交按钮时显示验证消息
我尝试在使用 class-invalid-feedback 的单选按钮之后使用另一个 div,它不会显示。
<div id="mb">
<input type="radio" id="h" value="hancock" name="mb" required>
<label for="h"><img src="static/hancock.jpg" class="img-thumbnail" alt="hancock"></label>
<input type="radio" id="s" value="shirahoshi" name="mb" required>
<label for="s"><img src="static/shirahoshi.png" class="img-thumbnail" alt="shirahoshi"></label>
<input type="radio" id="n" value="nami" name="mb" required>
<label for="n"><img src="static/nami.jpeg" class="img-thumbnail" alt="nami"></label>
<div class="invalid-feedback">Pick One!</div>
</div>
它不会显示无效的反馈文本。
编辑:片段 https://jsfiddle.net/uv51r3jw/5/ 我使用了表单的 novalidate 属性来禁用默认验证
解决方案
在您的 javascript 验证中添加了三行代码,以强制在未选择时显示无效反馈并在图像上添加一些样式
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function () {
'use strict';
window.addEventListener('load', function () {
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.getElementsByClassName('needs-validation');
// Loop over them and prevent submission
var validation = Array.prototype.filter.call(forms, function (form) {
form.addEventListener('submit', function (event) {
if (form.checkValidity() === false) {
if ($('input.Check[name=mb]:checked').length < 1) {
$(".img-thumbnail").css('outline', '1px solid red');
$(".invalid-feedback").show();
}
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
img
{
height:100px;
width:100px;
object-fit: cover;
}
<html>
<head>
<meta name="viewport" content="initial-scale=1, width=device-width">
<!-- http://getbootstrap.com/docs/4.1/ -->
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet">
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>
<title>snippet</title>
</head>
<body>
<form action="#" class="needs-validation" novalidate>
<div id="mb">
<input type="radio" id="h" value="hancock" name="mb" required>
<label for="h"><img src="https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=format%2Ccompress&cs=tinysrgb&dpr=1&w=500" class="img-thumbnail" alt="h"></label>
<input type="radio" id="s" value="shirahoshi" name="mb" required>
<label for="s"><img src="https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=format%2Ccompress&cs=tinysrgb&dpr=1&w=500" class="img-thumbnail" alt="s"></label>
<input type="radio" id="n" value="nami" name="mb" required>
<label for="n"><img src="https://images.pexels.com/photos/45201/kitty-cat-kitten-pet-45201.jpeg?auto=format%2Ccompress&cs=tinysrgb&dpr=1&w=500" class="img-thumbnail" alt="n"></label>
<div class="invalid-feedback">Pick One!</div>
</div>
<button class="btn btn-primary" type="submit" style="margin-left:11px">Submit</button>
</form>
</body>
</html>
推荐阅读
- c# - 如何使用 Dapper 将 DateTime 传递给存储过程?
- xml - 在 Oracle 10g 上加载超过 64K 的 XML
- gnuplot - 绘制多个文件以产生多个输出
- javascript - 将多个图像复制/粘贴到 ClipboardData(剪贴板 API)
- powershell - Powershell 中集群计划任务的每月触发器
- c# - C#:如何将数据从主文件发送到另一个文件并将它们影响到构造函数参数?
- php - LARAVEL:我想从另一个表中导入信息并将其添加到注册表单
- python - 使用 Selenium 抓取 Google Scholar 时提取 .text 会返回一个空字符串
- azure-devops - 用于评论的 Azure Boards 扩展,如树
- python - 如果条目满足特定条件python,则绘制函数