نمایش نتایج 1 تا 4 از 4

نام تاپیک: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت

  1. #1

    مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت

    درود
    این مسئله را خواهشمندم حل کرده و مشکل را مشخص فرمایید . مرسی
    دو اینپوت باکیس که با آی دی مشخص شده اند از کد زیر برای جداکننده ارقام استفاده می کنند و کار می کند:

    <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>



  2. #2

    نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت

    فقط خط 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='';


    این دومی کار نمیکند.

  3. #3

    نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت

    مشکل حل شد.
    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='';

  4. #4

    نقل قول: مشکل در کد ایجاد کننده جداکننده ارقام در جاواسکریپت

    کل کد میشود:

    <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>


تاپیک های مشابه

  1. سوال: مشکل در ایجاد Shockwave Flash Object با کد نویسی
    نوشته شده توسط rezamansori در بخش C#‎‎
    پاسخ: 0
    آخرین پست: پنج شنبه 11 خرداد 1391, 01:59 صبح
  2. سوال: مشکل در استفاده تابع جهت جدا کننده ارقام - Thousand separator
    نوشته شده توسط سمانه علوی فر در بخش C#‎‎
    پاسخ: 12
    آخرین پست: چهارشنبه 16 فروردین 1391, 12:27 عصر
  3. مشکل در کد ایجاد دیتابیس از داخل برنامه
    نوشته شده توسط saeedhushmand در بخش C#‎‎
    پاسخ: 1
    آخرین پست: دوشنبه 23 آبان 1390, 01:15 صبح
  4. سوال: مشکل در ایجاد ارتباط با بانک اطلاعاتی از طریق کد نویسی
    نوشته شده توسط mig-mig در بخش ASP.NET Web Forms
    پاسخ: 15
    آخرین پست: چهارشنبه 23 شهریور 1390, 19:12 عصر
  5. سوال: مشکل در ایجاد صفحه بوسیله نوشتن کد
    نوشته شده توسط esteftaats1368 در بخش طراحی وب (Web Design)
    پاسخ: 5
    آخرین پست: پنج شنبه 11 فروردین 1390, 20:20 عصر

برچسب های این تاپیک

قوانین ایجاد تاپیک در تالار

  • شما نمی توانید تاپیک جدید ایجاد کنید
  • شما نمی توانید به تاپیک ها پاسخ دهید
  • شما نمی توانید ضمیمه ارسال کنید
  • شما نمی توانید پاسخ هایتان را ویرایش کنید
  •