简体   繁体   中英

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.

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. 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 :

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 :

在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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