简体   繁体   English

如何在单击按钮时将 select 边框颜色更改为红色

[英]how to change select border color to red on button click

Hello i have a interesing question related to validation and forms as i decide to use my own class however i realize that when button is click the border color is not displaying anymore您好,我有一个与验证和 forms 有关的有趣问题,因为我决定使用我自己的 class 但是我意识到当单击按钮时,边框颜色不再显示

i have try test using css for invalid and it work so now i would want that if the user press the button the style for the select border be red as is invalid how can i do that here my code below我已经尝试使用 css 进行测试无效并且它可以工作,所以现在我希望如果用户按下按钮,select 边框的样式是红色的,因为它是无效的,我怎么能在下面的代码中做到这一点

 var myForm = document.getElementsByClassName('needs-validation'); var btn = document.querySelector("[type='submit']") btn.addEventListener("click", function() { if (myForm.checkValidity()) { ('.select-user-role').setCustomValidity('') } else { alert("no"); } }); // 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);
 .select-user-role:invalid { border-color: red; }
 <div class="form-group row"> <label for="validationRole" class="col-form-label role">*Role:</label> <div class="col-3 "> <select class="select-user-role custom-select-sm col-11" id="select-user-role" required> <option class="selectrole" selected disabled value="">Select....</option> </select> <div style="margin-left: 10px;" class="invalid-feedback"> Enter a valid user role! </div> </div> </div> <button type="submit" class="btn-primary submitbutton">Add</button>

This is my website of how it look like when is it in invalid这是我的网站,它何时无效在此处输入图像描述

What i want is this validation message and the border color red pop up我想要的是这个验证消息和边框颜色红色弹出

You can use Javascript to add a class to the select tag when the user is invalid.当用户无效时,您可以使用 Javascript 将 class 添加到 select 标签中。

Try the following尝试以下

var myForm = document.getElementsByClassName('needs-validation');
var btn = document.querySelector("[type='submit']")
var box = document.getElementsByClassName('select-user-role')
btn.addEventListener("click", function() {
  if (myForm.checkValidity()) {
    box.classList.remove("error");
    ('.select-user-role').setCustomValidity('')
  } else {
    box.classList.add("error");
    alert("no");
  }
});

Then for CSS, add然后对于 CSS,添加

.select-user-role.error {
  border-color: red;
}

I've even added remove, so when the selected user is valid the red border disappears我什至添加了删除,所以当所选用户有效时,红色边框消失

you haven't added a checkValidity function, so I've made something temporary你还没有添加checkValidity function,所以我做了一些临时的

onclick of the button, if validity is false then you'll give the user-role element the invalid class, if not it'll be removed按钮的 onclick,如果有效性为false ,那么您将为user-role元素提供invalid的 class,否则将被删除

 var myForm = document.getElementsByClassName('needs-validation'); var btn = document.querySelector("[type='submit']"); var userrole = document.querySelector('.select-user-role'); const checkValidity = () => { //your code return false; } btn.onclick = () => { if (checkValidity()) { userrole.classList.remove('invalid'); } else { userrole.classList.add('invalid'); } }
 .select-user-role.invalid { border-color: red; }
 <div class="form-group row"> <label for="validationRole" class="col-form-label role">*Role:</label> <div class="col-3 "> <select class="select-user-role custom-select-sm col-11" id="select-user-role" required> <option class="selectrole" selected disabled value="">Select....</option> </select> <div style="margin-left: 10px;" class="invalid-feedback"> Enter a valid user role! </div> </div> </div> <button type="submit" class="btn-primary submitbutton">Add</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM