是否可以将此jquery代码转换为javascript?
$("#field").keypress(function (evt) {
if (evt.which == 46) {
$(this).val($(this).val() + ',');
evt.preventDefault();
}
//The input of more than 2 numbers after the decimal point is prevented
var foo = $(this).val();
if( !foo.match(/^(\d)*,?(\d){0,1}$/) ){
evt.preventDefault();
}
});
提前Thanx!
答案 0 :(得分:1)
最后我找到了解决方案。希望它可以帮助任何人
<script type="text/javascript">
function decimals(that) {
var s = that.value;
var i = s.indexOf(".");
if (i < 0 || s.substr(i+1).length < 2) return;
alert("Only 1 digit to the right of the decimal are allowed!");
that.value = s.substring(0,i+2);
}
</script>
<input type="text" size="5" maxlength="5" onkeyup="decimals(this)">
祝福
答案 1 :(得分:0)
检查此代码http://jsfiddle.net/s62W5/可能会对您有所帮助
var x=document.getElementById("field");
x.onkeydown = function(e){
var keyPress;
if (typeof event !== 'undefined') {
keyPress = event.keyCode;
}
else if (e) {
keyPress = e.which;
}
if (keyPress == 46) {
x.value= x.value + ',';
}
};