مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت
درود
این مسئله را خواهشمندم حل کرده و مشکل را مشخص فرمایید . مرسی
دو اینپوت باکیس که با آی دی مشخص شده اند از کد زیر برای جداکننده ارقام استفاده می کنند و کار می کند:
<script type="text/javascript">
//remove comma from number
function removeCommas(str) {
return(str.replace(/,/g,''));
}
// insert commas as thousands separators
function addCommas(n){
var rx= /(\d+)(\d{3})/;
return String(n).replace(/^\d+/, function(w){
while(rx.test(w)){
w= w.replace(rx, '$1,$2');
}
return w;
});
}
// return integers and decimal numbers from input
// optionally truncates decimals- does not 'round' input
function validDigits(n, dec){
n= n.replace(/[^\d\.]+/g, '');
var ax1= n.indexOf('.'), ax2= -1;
if(ax1!= -1){
++ax1;
ax2= n.indexOf('.', ax1);
if(ax2> ax1) n= n.substring(0, ax2);
if(typeof dec=== 'number') n= n.substring(0, ax1+dec);
}
return n;
}
window.onload= function(){
var n1= document.getElementById('priceMin'),
n2= document.getElementById('priceMax');
n1.value = n2.value='';
n1.onfocus=n1.onkeyup= n1.onchange=n2.onfocus=n2.onkeyup=n2.onchange= function(e){
e=e|| window.event;
var who=e.target || e.srcElement,temp;
temp= validDigits(who.value,2);
who.value= addCommas(temp);
}
n1.onblur=n2.onblur= function(){
var temp=n1.value;
var temp2=n2.value;
if(temp)n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
}
n1.onkeypress=n2.onkeypress= function(event) {
if (event.which == 13 || event.keyCode == 13) {
var temp=n1.value;
var temp2=n2.value;
if(temp) n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
return false;
}
return true;
}
}
</script>
برای دو اینپوت باکس که آی دی ندارند ولی name دارند از کد زیر استفاده شده که شبیه همان کد با اندکی تغییر است ولی کار نمی کند مشکل چیست؟
<script type="text/javascript">
//remove comma from number
function removeCommas(str) {
return(str.replace(/,/g,''));
}
// insert commas as thousands separators
function addCommas(n){
var rx= /(\d+)(\d{3})/;
return String(n).replace(/^\d+/, function(w){
while(rx.test(w)){
w= w.replace(rx, '$1,$2');
}
return w;
});
}
// return integers and decimal numbers from input
// optionally truncates decimals- does not 'round' input
function validDigits(n, dec){
n= n.replace(/[^\d\.]+/g, '');
var ax1= n.indexOf('.'), ax2= -1;
if(ax1!= -1){
++ax1;
ax2= n.indexOf('.', ax1);
if(ax2> ax1) n= n.substring(0, ax2);
if(typeof dec=== 'number') n= n.substring(0, ax1+dec);
}
return n;
}
window.onload= function(){
var n1= document.getElementsByName('min_field_31'),
n2= document.getElementsByName('max_field_31');
n1.value = n2.value='';
n1.onfocus=n1.onkeyup= n1.onchange=n2.onfocus=n2.onkeyup=n2.onchange= function(e){
e=e|| window.event;
var who=e.target || e.srcElement,temp;
temp= validDigits(who.value,2);
who.value= addCommas(temp);
}
n1.onblur=n2.onblur= function(){
var temp=n1.value;
var temp2=n2.value;
if(temp)n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
}
n1.onkeypress=n2.onkeypress= function(event) {
if (event.which == 13 || event.keyCode == 13) {
var temp=n1.value;
var temp2=n2.value;
if(temp) n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
return false;
}
return true;
}
}
</script>
نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت
فقط خط 31 و 32 دو کد متفاوت است
window.onload= function(){
var n1= document.getElementById('priceMin'),
n2= document.getElementById('priceMax');
n1.value = n2.value='';
window.onload= function(){
var n1= document.getElementsByName('min_field_31'),
n2= document.getElementsByName('max_field_31');
n1.value = n2.value='';
این دومی کار نمیکند.
نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت
مشکل حل شد.
getElementsbyName یک آرایه بر می گرداند که شامل تمام عناصری با نام مشترک است.
پس باید کد را به شکل زیر تغییر دهیم
window.onload= function(){
var array01= document.getElementsByName('min_field_31'),
array02= document.getElementsByName('max_field_31');
n1=array01[0];
n2=array02[0];
n1.value = n2.value='';
نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت
کل کد میشود:
<script type="text/javascript">
//remove comma from number
function removeCommas(str) {
return(str.replace(/,/g,''));
}
// insert commas as thousands separators
function addCommas(n){
var rx= /(\d+)(\d{3})/;
return String(n).replace(/^\d+/, function(w){
while(rx.test(w)){
w= w.replace(rx, '$1,$2');
}
return w;
});
}
// return integers and decimal numbers from input
// optionally truncates decimals- does not 'round' input
function validDigits(n, dec){
n= n.replace(/[^\d\.]+/g, '');
var ax1= n.indexOf('.'), ax2= -1;
if(ax1!= -1){
++ax1;
ax2= n.indexOf('.', ax1);
if(ax2> ax1) n= n.substring(0, ax2);
if(typeof dec=== 'number') n= n.substring(0, ax1+dec);
}
return n;
}
window.onload= function(){
var array01= document.getElementsByName('min_field_31'),
array02= document.getElementsByName('max_field_31');
n1=array01[0];
n2=array02[0];
n1.value = n2.value='';
n1.onfocus=n1.onkeyup= n1.onchange=n2.onfocus=n2.onkeyup=n2.onchange= function(e){
e=e|| window.event;
var who=e.target || e.srcElement,temp;
temp= validDigits(who.value,2);
who.value= addCommas(temp);
}
n1.onblur=n2.onblur= function(){
var temp=n1.value;
var temp2=n2.value;
if(temp)n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
}
n1.onkeypress=n2.onkeypress= function(event) {
if (event.which == 13 || event.keyCode == 13) {
var temp=n1.value;
var temp2=n2.value;
if(temp) n1.value=(removeCommas(temp));
if(temp2)n2.value=(removeCommas(temp2));
return false;
}
return true;
}
}
</script>