2013-12-16 25 views
-1

我正在使用Javascript [僅限初學者]計算點的「剩餘點」。Javascript:keypress/keycode

樣品GUI:

enter image description here

場景:

我輸入22 spots然後後,我點擊txtA的Remaining Spots is already "22" [it should be "21"]之外txtA I input 1。但是當我試圖change the value of txtA to "2"現在剩下的景點已經是"21" [it should be "20"]

代碼爲我的ASP.NET

protected void txtA_TextChanged(object sender, EventArgs e) 
{ 
    txtRemaining.Text = (Convert.ToInt32(txtSlot.Text) - Convert.ToInt32(txtA.Text)).ToString(); 
} 

protected void txtSlot_TextChanged(object sender, EventArgs e) 
{ 
    int slot= Convert.ToInt32(txtSlot.Text); 
    txtRemaining.Text = Convert.ToString(slot); 
} 

的Javascript:

function checkIt(evt) { 
evt = (evt) ? evt : window.event 
var charCode = (evt.which) ? evt.which : evt.keyCode 

if (charCode > 31 && (charCode < 48 || charCode > 57)) { 
    return false 
} 
status = "" 
var comp1= document.getElementById("<%= __txt67Value1.ClientID %>").value; 
var comp2 = document.getElementById("<%= txtSlot.ClientID %>").value; 
document.getElementById("<%= txtRemaining.ClientID %>").value = comp2- comp1; 
return true 
} 

在此先感謝...

+0

你如何觸發'checkIt'? - 標題不僅僅是一系列關鍵字,請讓它更好一點...並用瀏覽器調試您的代碼 – Aristos

回答

0

這裏是的jsfiddle:http://jsfiddle.net/kwPy4/

var $comp1 = document.getElementById("<%= __txt67Value1.ClientID %>"); 
var $comp2 = document.getElementById("<%= txtSlot.ClientID %>"); 

function CheckIt() { 
    var resultSlot = $comp2.val() - $comp1.val(); 
    $comp1.val(resultSlot); 
} 

$(document).on("blur", "<%= __txt67Value1.ClientID %>", function() { 
    CheckIt(); 
}); 
+0

非常感謝您。 :) –