2013-12-16 24 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

非常感谢您。 :) –