admin管理员组文章数量:1291135
I have an input that needs to be a numerical digit and needs to update a label when the value is changed.
<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)">
The onkeypress function is needed to ensure that the user can only enter in numbers, and the onchange will update my label with the new information.
The onkeypress function is working great, I am only able to type numbers. However, the onchange function is doing nothing.
validation function:
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;};
Update Function:
this.update_shares = function(o){
alert("test");
$(".dollar_amount").html("$" + "value" + ".00");};
I have an input that needs to be a numerical digit and needs to update a label when the value is changed.
<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)">
The onkeypress function is needed to ensure that the user can only enter in numbers, and the onchange will update my label with the new information.
The onkeypress function is working great, I am only able to type numbers. However, the onchange function is doing nothing.
validation function:
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;};
Update Function:
this.update_shares = function(o){
alert("test");
$(".dollar_amount").html("$" + "value" + ".00");};
Share
Improve this question
asked May 5, 2013 at 18:56
gamesfrekegamesfreke
1051 gold badge1 silver badge9 bronze badges
1
-
jQuery will do all the needed translations,
evt.which
is quite enough. – Roko C. Buljan Commented May 5, 2013 at 19:03
2 Answers
Reset to default 3Since you're using jQuery, you might as well use the jQuery event handlers as well:
HTML
<input id="shares" name="shares" type="text" maxlength="3" />
<div class="dollar_amount"></div>
JS
$("#shares").on("keydown", function (evt) {
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
return true;
});
$("#shares").on("change", function () {
var o = $(this).val();
$(".dollar_amount").html("$" + o + ".00");
});
Working example: http://jsfiddle/charlescarver/e43pE/1/
Does this suit your needs? Or are you needing to keep this.update_shares = function(o)
?
Therefore, what you want is update the label as the value changes. If I were you, I would do it on the keypress event. I mean, something like:
this.isNumberKey = function(evt){
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)){
return false;
}
$(".dollar_amount").html("$" + "value" + ".00");};
return true;
};
本文标签: javascriptIssues using onchange() and onkeypress() togetherStack Overflow
版权声明:本文标题:javascript - Issues using onchange() and onkeypress() together - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741522901a2383293.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论