我有一個輸入需要是數字數字,並且需要在值更改時更新標籤。使用onchange()和onkeypress()一起使用的問題
<input id="shares" name="shares" type="text" maxlength="3" onchange="game_board.update_shares(this);this.oldvalue = this.value;" onkeypress="return game_board.isNumberKey(event)">
需要onkeypress函數來確保用戶只能輸入數字,onchange會用新信息更新我的標籤。
onkeypress函數工作得很好,我只能輸入數字。但是,onchange函數什麼都不做。
驗證功能:
this.isNumberKey = function(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57)){
return false;
}
return true;};
更新功能:
this.update_shares = function(o){
alert("test");
$(".dollar_amount").html("$" + "value" + ".00");};
jQuery將完成所有需要*翻譯*,'evt.which'就夠了。 – 2013-05-05 19:03:09