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

时间:2016-08-09 13:16:02

标签: javascript twitter-bootstrap styling bootstrap-select

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

我尝试了这个但是没有用。

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

的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

<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>

2 个答案:

答案 0 :(得分:2)

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

您可以覆盖CSS

  

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

希望这有帮助

更新: 通过使用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>

答案 1 :(得分:1)

如果您看到bootstrap-select生成的DOM元素,它实际上会隐藏原始select元素并使用divspan和{{1}创建自己的结构}。没有提供任何官方文档来更改其buttons,但作为解决方法,您可以执行以下操作:

您需要更改由border生成的buttonclass的{​​{1}}的边框。由于创建的元素不会被赋予任何btn dropdown-toggle btn-default,我们将使用bootstrap-select id

class

<强>更新

评论中的解释。

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