gpt4 book ai didi

jquery - 引导多选 : How to change backgroung color if None selected

转载 作者:行者123 更新时间:2023-12-01 03:08:09 25 4
gpt4 key购买 nike

我在网络表单中使用了 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>

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

最佳答案

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();
});
<!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>

关于jquery - 引导多选 : How to change backgroung color if None selected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40460251/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com