我有一个表单,有4个选项(它们可能是复选框或单选)。
我想选择多个选项,但其中一个是必选的。
我知道这在JS/jQuery中是可能的,但我想要一个基于HTML/CSS的解决方案。
发布于 2015-11-21 19:25:12
您可以使用以下内容,其中一个是必需的。
<input type="radio" name="name" required>
无论是否勾选,都不会测试哪一个不是必填项。
发布于 2015-11-30 10:37:42
试试这个:
<input id="c3" type="checkbox" required><label for="c3">Third</label><br>
<input id="c4" type="checkbox" required><label for="c4">Fourth</label><br>
或者您可以尝试使用jquery来验证html复选框:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Makes "field" always required. Nothing and blanks are invalid. </title>
<link rel="stylesheet" href="http://jqueryvalidation.org/files/demo/site-demos.css">
</head>
<body>
<form id="myform">
<label for="field">Required: </label>
<input type="text" class="left" id="field" name="field">
<br/>
<input type="submit" value="Validate!">
</form>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"> </script>
<script src="http://jqueryvalidation.org/files/dist/additional- methods.min.js"></script>
<script>
// just for the demos, avoids form submit
jQuery.validator.setDefaults({
debug: true,
success: "valid"
});
$( "#myform" ).validate({
rules: {
field: {
required: true
}
}
});
</script>
</body>
</html>
required
是html验证事物的方式。
https://stackoverflow.com/questions/33842700
复制相似问题