2016-04-26 123 views
0

在js GetTotal()函數中,我有一個確認框,後跟一個IF語句,它或者(1)計算新的總數(如果用戶選擇'ok');或(2)將變量'input1'和'input2'重新加載到文本輸入框(如果用戶選擇'取消')。當'input1'和'input2'重新加載時,即使用戶之前已經更改了這些變量,它們也總是重置爲'input1'(5555)和'input2'(666)的原始全局值。本地全局變量問題 - javascript

我希望爲'input1'和'input2'重新加載最近輸入的變量(這與所顯示的總數一致),而不是總是恢復爲'input1'的原始全局值5555)和'輸入2'(666)。我認爲這與本地和全球有關,但我無法弄清楚。感謝任何建議。麥克風。

<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() { 
total = 0 
    BootstrapDialog.confirm('Are you sure you want to do this?', function(result){ 
    if(result) { 
     $('input[type=text]').each(function(index, value) { 
      total += parseInt($(value).val() || 0); 
     }); 
     $("#chkTotal").html(total); 
    }else{document.getElementById("input1").value = input1; 
    document.getElementById("input2").value = input2;} 
}); 
} 

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

尚不完全清楚你的意思通過*「當'input1'和'input2'重新加載」*。你的意思是當頁面刷新時?因爲這是正常的,因爲它也重新加載腳本,並且不知道在刷新頁面之前發生了什麼...... –

+0

如果最終用戶在確認框中選擇「取消」,ELSE語句會重新填充具有變量的表單輸入'input1'和'input2'。在這種情況下,出現的input1和input2的值始終是在代碼的JavaScript段開始處聲明的原始值(即'input1'= 5555和'input2'= 666)。當表單重新填充時,我想使用'input1'和'input2'的最新值替代原始值 – MikeMcClatchie

回答

0

我認爲,所有你需要做的是設置input1input2到其對應的文本框的值GetTotal

function GetTotal() { 
    total = 0 
    BootstrapDialog.confirm('Are you sure you want to do this?', function(result){ 
    if(result) { 
    input1 = parseInt($('#input1').val() || 0, 10); 
    input2 = parseInt($('#input2').val() || 0, 10); 
    // note that since you only have two fields, it's just as easy to write 
    total = input1 + input2; 
    //$('input[type=text]').each(function(index, value) { 
    // total += parseInt($(value).val() || 0); 
    //}); 
    $("#chkTotal").html(total); 
    }else{ 
    document.getElementById("input1").value = input1; 
    document.getElementById("input2").value = input2; 
    } 
}); 
+0

請注意,我使用'parseInt(value,radix)',使用10作爲基數。這不是必須的,但是如果用戶輸入「010」,由於「0」表示八進制,他們在某些早期版本的JavaScript中不會得到10。 –

+0

歡呼!這是一種享受 – MikeMcClatchie

0

您需要在最終用戶輸入值時更新變量。東西這樣做的效果可能工作:

$('#input1').on('change blur', function() { 
    input1 = Number(this.value); 
});