twitter-bootstrap 如何更改 Bootstrap-select 的边框颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38851856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 00:12:19  来源:igfitidea点击:

How to change border color of Bootstrap-select

javascripttwitter-bootstrapstylingbootstrap-select

提问by kevin_b

For a custom framework like Bootstrap select https://silviomoreto.github.io/bootstrap-select/how do I change the border color of a select box in Javascript?

对于像 Bootstrap 这样的自定义框架,选择https://silviomoreto.github.io/bootstrap-select/如何在 Javascript 中更改选择框的边框颜色?

I tried this but does not work.

我试过这个,但不起作用。

document.getElementById("box").style.borderColor = "red";

Javascript

Javascript

function validate() {
    var ok = true;

    var box = document.getElementById("box").value;

    if (!box) {
        document.getElementById("box").style.borderColor = "red";
        ok = false;
    } 

    return ok;
}

HTML

HTML

<form action="#" onsubmit="return validate()" method="POST">
    <select id="box" name="num" class="selectpicker form-control">
      <option value="" selected="selected">select number</option>
      <option value="1">1</option>
      <option value="2">2</option>
    </select>

    <div class="text-center">
        <button type="submit" class="btn btn-primary">submit</button>
    </div>
</form>

采纳答案by Guruprasad Rao

If you see the DOM element generated by bootstrap-select, it actually hides the original selectelement and creates its own structure with div,spanand buttons. There isn't any official documentation provided to change its border, but as a workaround you could do it as below:

如果您看到由 生成的 DOM 元素bootstrap-select,它实际上隐藏了原始select元素并使用div,span和创建了自己的结构buttons。没有提供任何官方文档来更改其border,但作为一种解决方法,您可以按照以下方式进行:

You need to change border of the buttonwhose classis btn dropdown-toggle btn-default, which is generated by bootstrap-select. Since the element created will not be given any idwe will make use of its classwith document.getElementsByClassName("classnames")[0]

你需要的变化边界button,其classbtn dropdown-toggle btn-default,这是由产生bootstrap-select。由于创建的元素将不会得到任何id我们将利用其classdocument.getElementsByClassName("classnames")[0]

document.getElementsByClassName("btn dropdown-toggle btn-default")[0].style.borderColor = "red";


Update

更新

Explanation in comments.

评论中的解释。

$(document).ready(function() {
  //add a change event to each selectpicker
  $('.selectpicker').selectpicker().on('change', function() {
    var id = $(this).attr('id'); //get current element's id
    var selected = $(this).find("option:selected").val(); //get current element's selected value
    var condition = false; //default false to form valid
    $(this).selectpicker('setStyle', 'btn-danger', selected == "" ? "add" : "remove");
    if (id == "box") {//if its first select box
      condition = !(selected == 2 || selected == ""); //check if its value==2 or value=="" so that form validation depends of respective selects
      $("#box2").selectpicker(selected == 2 ? "show" : "hide").val("");
      //show or hide box2 based on selected val
      $("#box2").selectpicker('setStyle', 'btn-danger',"remove");
      //remove danger from 2nd selectpicker when its hidden or shown
    } else {
      condition = !(selected == "");
      //if its box 2 check if any value is selected
    }
    $(this).closest('form').data('valid', condition);
    //set it to form's data-valid attribute
  });
  $("#box2").selectpicker('hide');//default hide on page load
  
  //form submit event instead of validate() inline function
  $("form").on("submit", function(e) {
    e.preventDefault();//prevent default action of submit
    var isValid = $(this).data('valid');//check if its valid
    if (!isValid) {
      //if not valid loop through each selectpicker
      $.each($('.selectpicker'),function(){
        var selected=$(this).val();
        //check appropriate values for each select and set/remove the btn-danger class for respective selectpickers
        $(this).selectpicker('setStyle', 'btn-danger', selected == "" ? "add" : "remove"); 
      })
    }
    else{
      alert(isValid);
      //Submit your form
    }
  })
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/css/bootstrap-select.min.css" />


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-select/1.10.0/js/bootstrap-select.min.js"></script>

<!--add data-valid attribute to form to make sure its valid or not and default to false-->

<form action="#" data-valid="false" method="POST">
  <select id="box" name="num" class="selectpicker form-control">
    <option value="" selected="selected">select number</option>
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
  <select id="box2" name="num" class="selectpicker form-control">
    <option value="" selected="selected">select number</option>
    <option value="1">1</option>
    <option value="2">2</option>
  </select>
  <div class="text-center">
    <button type="submit" class="btn btn-primary">submit</button>
  </div>
</form>

回答by GauravKP

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="https://silviomoreto.github.io//css/highlight.css" rel="stylesheet">
    <link href="https://silviomoreto.github.io//css/base.css" rel="stylesheet">
    <link href="https://silviomoreto.github.io//css/custom.css" rel="stylesheet">
    <link href="https://silviomoreto.github.io//dist/css/bootstrap-select.min.css" rel="stylesheet">

    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>


</head>
<body>
    <form id="form1" runat="server">
        <div class="row">
            <div class="col-xs-3">
                <div class="form-group">

                    <select id="box" name="num" class="selectpicker form-control" data-style="warning">

                        <option value="" selected="selected">Select number</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                    </select>

                    <input type="button" style="width: 180px; height: 30px" onclick="changeColor()" value="Validate"/>
                </div>
            </div>
        </div>
    </form>

    <script>
        function changeColor() {
            document.getElementById("box").style.borderColor = "red";
            document.getElementById("box").style.borderWidth = "1px";
            return false;
        }

    </script>

</body>
</html>

You can override the CSS

您可以覆盖 CSS

.bootstrap-select{ border: 1px solid red //sample }

.bootstrap-select{ 边框:1px 实心红色 //sample }

Hope this helps

希望这可以帮助

Update: By using JavaScript, this can be done as shown below (considering all required JS file as added already)

更新:通过使用 JavaScript,可以按如下所示完成(考虑已添加所有必需的 JS 文件)

<form id="form1" runat="server">
        <div class="row">
            <div class="col-xs-3">
                <div class="form-group">

                    <select id="box" name="num" class="selectpicker form-control" data-style="warning">

                        <option value="" selected="selected">Select number</option>
                        <option value="1">1</option>
                        <option value="2">2</option>
                    </select>

                    <input type="button" style="width: 10px; height: 30px" onclick="changeColor()" value="Validate"></input>
                </div>
            </div>
        </div>
    </form>

    <script>
        function changeColor() {
            document.getElementById("box").style.borderColor = "red";
            document.getElementById("box").style.borderWidth = "1px";
            return false;
        }

    </script>