2016-04-25 122 views
-1

我在试图构建一个jacvascript运行总计算器,它允许用户指定被起诉的输入来计算显示在屏幕上的(总和)总和。javascript确认框if语句

在用户对输入进行更改之前,我想要一个确认框,允许用户选择好(这导致新的总数正在计算)或取消。

我已将带有IF语句的确认框代码插入到GetTotal函数中,但似乎没有工作。无论用户是选择好还是取消,每次计算新总数。任何帮助不胜感激。迈克

<head> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.35.1/js/bootstrap-dialog.min.js"></script> 
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" /> 
</head> 

<script> 
var input1 = 5555; 
var input2 = 666; 

$(document).ready(function() { 
    document.getElementById("input1").value = input1; 
    document.getElementById("input2").value = input2; 
    GetFirstTotal(); 
}); 

function GetFirstTotal() { 
    var total = 0; 
    $('input[type=text]').each(function(index, value) { 
     total += parseInt($(value).val() || 0); 
    }); 

    $("#chkTotal").html(total); 
} 

function GetTotal() { 
    var total = 0; 
BootstrapDialog.confirm('Are you sure you want to do this?'); 
if(confirm("text")==1) 
{ 
    $('input[type=text]').each(function(index, value) { 
     total += parseInt($(value).val() || 0); 
    }); 

    $("#chkTotal").html(total); 
} 
else {}} 
</script> 

TOTAL: 
<div id="chkTotal"></div> 
<br> 
<input type="text" name="input1" id="input1"/> 
<input type="button" value="Change X" onclick="GetTotal(this)"/> 
<br> 
<br> 
<input type="text" name="input2" id="input2"/> 
<input type="button" value="Change Y" onclick="GetTotal(this)"/> 
+1

我扔掉问我,如果你真的想要它做的工作任何计算器。 – kay

回答

1

默认的Javascript confirm()函数应返回一个布尔值,所以你应该只能够使用:

if(confirm('Are you sure you want to do this?')) 
{ 
     // Do these things 
     $('input[type=text]').each(function(index, value) { 
      total += parseInt($(value).val() || 0); 
     }); 
     $("#chkTotal").html(total); 
} 

但是,看来,你正在使用的BootstrapDialog插件,其中似乎以不同的方式操作了一下,接受回调,检查已输入的值:

enter image description here

所以,你的代码可能会是这个样子,如果你想只使用它作为一个选项确认:

BootstrapDialog.confirm('Are you sure you want to do this?', function(result){ 
    // If result is true, then the user confirmed the message 
    if(result) { 
     // Do work here 
     $('input[type=text]').each(function(index, value) { 
      total += parseInt($(value).val() || 0); 
     }); 
     $("#chkTotal").html(total); 
    } 
}); 
+0

谢谢!那完美的作品 – MikeMcClatchie