2016-03-25 6 views
0

jquery 1.6.4で使用すると動作しませんが、新しいバージョンで正常に動作します。 Becasue私はこれが動作すると信じて、私はエラーこの関数を古いjqueryで動作させるにはどうすればよいですか?

TypeError: $(…).on is not a function

http://jsfiddle.net/w0jekdL6/18/

$(".amount").on("keyup", function(){ 
    var valid = /^\d{0,5}(\.\d{0,2})?$/.test(this.value), 
     val = this.value; 
    var pos = this.selectionStart - 1; 

    if(!valid){ 
     if(typeof this.lastValid != "undefined") { 
      this.value = this.lastValid 
      this.setSelectionRange(pos, pos); 
     } else { 
      this.value = ""; 
     } 
    } else { 
     this.lastValid = val; 
    } 
}); 
+1

で追加された本当の答えは、jQueryの新しいバージョンを使用することで、数年でバージョンを使用する理由はありません古い。 – adeneo

+0

私はそれを試みましたが、残念ながらそれは他のものを壊します。 –

+0

次に、他のものを新しいものに置き換えるべきでしょう。 – adeneo

答えて

1

利用.keyup()の代わり.on()を見ます:(the docsによると、これはjQueryの新しいバージョンのためのLSO作業)

$(".amount").keyup(function(){ 
    var valid = /^\d{0,5}(\.\d{0,2})?$/.test(this.value), 
     val = this.value; 
    var pos = this.selectionStart - 1; 

    if(!valid){ 
     if(typeof this.lastValid != "undefined") { 
      this.value = this.lastValid 
      this.setSelectionRange(pos, pos); 
     } else { 
      this.value = ""; 
     } 
    } else { 
     this.lastValid = val; 
    } 
}); 

jQueryの.on()が唯一のバージョン1.7

0

を取得し、古いjqueryのバージョンを使用している - jQuery Docs

$(".amount").keyup(function() {...});

関連する問題