我有一个执行数据验证的 JavaScript 函数,问题是使用警报在数据错误时发出警告。我想要做的是显示 Bootstrap 模式窗口以查看错误。这就是我所做的,它不起作用。
var contador = somevalue;
window.validar = function (cual){
if(cual.value == -1){
}
if($("#muestragrafico1").is(':checked')){
var cantidad = 3;
}else{
var cantidad = 1;
}
if(cual.checked){
contador++;
}else{
contador--;
}
if(contador <= cantidad){
return true;
}else{
if (cantidad == 3) {
//alert("Solo puede seleccionar un máximo de "+cantidad+" sensores.");
$('#myModalExito').modal('show');
}
if (cantidad == 1) {
alert("Solo puede seleccionar un máximo de "+cantidad+" sensores.");
}
contador--;
return false;
}
}`
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><?php echo L::Ticket_Succes?></h4> </div>
</div>
</div>
最佳答案
您可以如下动态显示错误。
动态创建模态的引用是 here .
function showError(message, type) {
BootstrapDialog.show({
title: (type == 1 ? 'Warning' : 'Error') + ' message',
message: message,
buttons: [{
label: 'Close',
cssClass: (type == 1 ? 'btn btn-warning' : 'btn btn-danger'),
action: function(dialog) {
dialog.close();
}
}]
});
}
function validationFunction(i){
if(i==0)
{
showError('This is error without type');
return false;
}
else if(i==1)
{
showError('This is error with type id 0 (Error)(Default)',0);
return false;
}
else if(i==2)
{
showError('This is error with type id 1 (Warning)',1);
return false;
}
}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/css/bootstrap-dialog.min.css">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.7/js/bootstrap-dialog.min.js"></script>
<button onclick="validationFunction(0)">Error modal 1</button>
<button onclick="validationFunction(1)">Error modal 2</button>
<button onclick="validationFunction(2)">Warning modal 1</button>
关于javascript - 从 js 函数调用引导模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41596850/