$.fn.numeral = function() {
$(this).css("ime-mode", "disabled");
this.bind("keypress",function() {
if (event.keyCode == 46) {
if (this.value.indexOf(".") != -1) {
return false;
}
} else {
return event.keyCode >= 46 && event.keyCode <= 57;
}
});
this.bind("blur", function() {
if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
this.value = this.value.substr(0, this.value.length - 1);
} else if (isNaN(this.value)) {
this.value = "";
}
});
this.bind("paste", function() {
var s = clipboardData.getData('text');
if (!/\D/.test(s));
value = s.replace(/^0*/, '');
return false;
});
this.bind("dragenter", function() {
return false;
});
this.bind("keyup", function() {
if (/(^0+)/.test(this.value)) {
this.value = this.value.replace(/^0*/, '0');
}
});
};上面JQ是输入 数值的类型!我把this.value = this.value.substr(0, 2); (目的:想保留1位小数 1.56 --> 1.5)不需要四舍五入 不起作用(有点错误)请JQ高手帮忙下:输入小数时候仅保留1位小数,如何修改! 谢谢!
$(this).css("ime-mode", "disabled");
this.bind("keypress",function() {
if (event.keyCode == 46) {
if (this.value.indexOf(".") != -1) {
return false;
}
} else {
return event.keyCode >= 46 && event.keyCode <= 57;
}
});
this.bind("blur", function() {
if (this.value.lastIndexOf(".") == (this.value.length - 1)) {
this.value = this.value.substr(0, this.value.length - 1);
} else if (isNaN(this.value)) {
this.value = "";
}
});
this.bind("paste", function() {
var s = clipboardData.getData('text');
if (!/\D/.test(s));
value = s.replace(/^0*/, '');
return false;
});
this.bind("dragenter", function() {
return false;
});
this.bind("keyup", function() {
if (/(^0+)/.test(this.value)) {
this.value = this.value.replace(/^0*/, '0');
}
});
};上面JQ是输入 数值的类型!我把this.value = this.value.substr(0, 2); (目的:想保留1位小数 1.56 --> 1.5)不需要四舍五入 不起作用(有点错误)请JQ高手帮忙下:输入小数时候仅保留1位小数,如何修改! 谢谢!
function sugarToTal(arg) {
var value = arg.toString().replace(/(\.\d)\d+/ig, "$1");
var xsd = value.toString().split(".");
if (xsd.length == 1) {
value = value.toString() + ".0";
}
return value;
}var val=99.08999999999999;
alert(sugarToTal(val));
alert(Math.floor(val*10)/10);
alert(Math.round(val*10)/10);
var b = "";
if (a.indexOf(".") > 0) {
b = a.substring(0, a.indexOf(".") + 2);
}
alert(b);//输出123.4
貌似substr不支持int行,lz要先把数字转换成字符串才可以