-1
직접 필드 가격 확인 번호를 입력하고 점을 입력하고 쉼표를 붙인 경우 점으로 대체하십시오. 변경 기능으로모든 필드에 대해 jquery change 함수를 사용하십시오.
나는 점 그리고 내가 그럼 내가 다른 입력에서 호출 할 수있는 하나 개의 함수를 작성하려고
.toFixed (N)를 사용하는 쉼표를 변경합니다. 20 개의 입력 변경 기능을 20 개 작성하는 것은 좋지 않습니다.
HTML :
<input id="txtId" type="text" onkeyup="NumAndTwoDecimals(event , this);"></input>
<br>
<input id="txtId2" type="text" onkeyup="NumAndTwoDecimals(event , this);"></input>
JS
$('#txtId').change(function(event) {
var newVal = String($(this).val()).replace(/\,/g,'.');
var regexp = /^([0-9]+[\.]?[0-9]?[0-9]?|[0-9]+)$/g;
if (regexp.test(newVal)) {
$(this).val(Number(newVal).toFixed(2));
}else{
// error
$(this).val('');
}
});
window.NumAndTwoDecimals = function(e , field) {
var val = field.value;
var re = /^([0-9]+[\,||\.]?[0-9]?[0-9]?|[0-9]+)$/g;
var re1 = /^([0-9]+[\,||\.]?[0-9]?[0-9]?|[0-9]+)/g;
if (re.test(val)) {
//do something here
} else {
val = re1.exec(val);
if (val) {
field.value = val[0];
} else {
field.value = "";
}
}
}
데모 :
사용 시작처럼 선택과 시작 '.each' ... 여기를 참조하십시오 : [.each()] (http://api.jquery.com/jquery.each/) – j809
또는 [이벤트 위임] (http://learn.jquery.com/events/event-delegation/) –