Bootstrap Multiselect:如果选择None,如何更改背景颜色

时间:2016-11-07 07:53:30

标签: jquery bootstrap-multiselect

我在我的网络表单中使用了Bootstrap Multiselect。我对所有输入/选择字段进行了验证检查。如果表单提交任何空输入或禁用选项或无选择,那么所有空字段都会将背景颜色更改为红色。似乎所有的输入和选择工作正常。但是选择多重选择我没有改变颜色。这里只是多选的代码部分:



if($("#example-optionClass").val() == null){
    $("#example-optionClass").css('background-color','red');
}

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <!-- Include the plugin's CSS and JS: -->
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/js/bootstrap-multiselect.js"></script>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-multiselect/0.9.13/css/bootstrap-multiselect.css" type="text/css"/>
        <script type="text/javascript">
            $(document).ready(function () {
                $('#example-optionClass').multiselect({
                    optionClass: function (element) {
                        var value = $(element).val();

                        if (value % 2 == 0) {
                            return 'even';
                        }
                        else {
                            return 'odd';
                        }
                    }
                });
            });
        </script>
        <style type="text/css">
            #example-optionClass-container .multiselect-container li.odd {
                background: #eeeeee;
            }
        </style>
    </head>
    <body>

        <div class="container">
            <h2>Bootstrap Multiselect Test</h2>
            <p>I want to change the Multiselect <b>background color</b>  If 'None Selected'</p>            
            <div id="example-optionClass-container">
                <select id="example-optionClass" multiple="multiple">
                    <option value="1">Option 1</option>
                    <option value="2">Option 2</option>
                    <option value="3">Option 3</option>
                    <option value="4">Option 4</option>
                    <option value="5">Option 5</option>
                    <option value="6">Option 6</option>
                </select>
            </div>
        </div>

    </body>
</html>
&#13;
&#13;
&#13;

我需要为此编写什么简单的jquery?请帮忙。感谢

1 个答案:

答案 0 :(得分:3)

TypedArray bgImgs = getResources().obtainTypedArray(R.array.background_name);

imgView.setImageResource(bgImgs.getResourceId(0, -1));// Here pass your index 
function onchange_multi(){
  setTimeout(function(){ // use timeout because master script run and your script run
    var btn_def = $("#example-optionClass-container").find('.btn-default');
    if($("#example-optionClass").val() === null){
       btn_def.addClass('btn-danger')  //btn-danger default class of bootstrap
    }else{
       btn_def.removeClass('btn-danger')  
    }
  },50);
}


//onload
onchange_multi();

//onchange
$('#example-optionClass').on('change',function(){
  onchange_multi();
});