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
Add a ment  | 

2 Answers 2

Reset to default 3

Since 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