简体   繁体   English

带有if语句的javascript确认框

[英]javascript confirm box with if statement

I'm trying to build a jacvascript running total calculator which allows the user to specify inputs which are sued to calculate a (sum) total which is shown on screen.我正在尝试构建一个 jacvascript 运行总计计算器,它允许用户指定用于计算屏幕上显示的(总和)总计的输入。

Before the user makes a change to the input I want to have a confirm box which allows the user to select okay (which leads to the new total being calculated) or cancel.在用户对输入进行更改之前,我想要一个确认框,允许用户选择好(这会导致计算新的总数)或取消。

I've inserted the confirm box code with an IF statement into the GetTotal function but it doesn't seem to be working.我已将带有 IF 语句的确认框代码插入到 GetTotal 函数中,但它似乎不起作用。 Every time the new total is calculated irrespective of whether the user selects okay or cancel.无论用户选择确定还是取消,每次都会计算新的总数。 Any help greatly appreciated.非常感谢任何帮助。 Mike麦克风

<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)"/>

The default Javascriptconfirm() function should return a boolean value, so you should just be able to use :默认的 Javascriptconfirm()函数应该返回一个布尔值,所以你应该能够使用:

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

However, it appears that you are using the BootstrapDialog plug-in, which seems to operate a bit differently and accepts a callback to check the value that was entered :但是,您似乎正在使用BootstrapDialog插件,它的操作似乎有点不同,并接受回调以检查输入的值:

在此处输入图片说明

So your code would likely look something like this, if you wanted to use it exclusively as a confirmation option :因此,如果您想将其专门用作确认选项,则您的代码可能看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM