【发布时间】:2019-04-16 19:00:42
【问题描述】:
我有这两个单选按钮,要求用户选择两个单选按钮之一。我一直在试图弄清楚如何设置单选按钮的样式,使其看起来更像常规按钮。我如何将这些单选按钮设置为看起来像普通按钮但同时保持单选按钮行为和表单验证?
此外,我的大部分样式都使用引导程序,以防不清楚。
// 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) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
}, false);
})();
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<form class="needs-validation" novalidate>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="radio" name="choice" id="emailConsentRadio" value="save" required>
<label class="form-check-label btn btn-outline-primary" for="save">
save
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="choice" id="emailConsentRadio" value="cancel" required>
<label class="form-check-label btn btn-outline-danger" for="cancel">
cancel
</label>
<div class="invalid-feedback">
select one option
</div>
</div>
</div>
<button type="submit" name="signup_signup" class="btn btn-primary btn-block" aria-describedby="signup_notes">Submit</button>
</form>
【问题讨论】:
标签: javascript jquery html css bootstrap-4