2013-08-01 4 views
3

나는 다음과 같은 코드를 추가하면 내 페이지의 형식이 잘못되었습니다 : 당신이 할 수 http://jsfiddle.net/Lwv4U/14/자바 스크립트/jQuery를 페이지의 서식을 나누기

다음 jsfiddle 사용이 코드를 보여

$('#Inventory_accountNumber').blur(function(){ 
    var accounts = $(this).val(); 
    accounts = accounts.replace(/-/g,''); 
    var accountNum = []; 
    accountNum = accounts.split(","); 
    for(var i=0;i<accountNum.length;i++) { 
     var newstr = ''; 
     if(accountNum[i].length == 24) { 
      newstr += accountNum[i].substring(0,4) + '-'; 
      newstr += accountNum[i].substring(4,7) + '-'; 
      newstr += accountNum[i].substring(7,10) + '-'; 
      newstr += accountNum[i].substring(10,14) + '-'; 
      newstr += accountNum[i].substring(14,20) + '-'; 
      newstr += accountNum[i].substring(20,24) + '-'; 
      newstr += '0000-000'; 
      accountNum[i] = newstr; 
     } 
     else if(accountNum[i].length == 31) { 
      newstr += accountNum[i].substring(0,4) + '-'; 
      newstr += accountNum[i].substring(4,7) + '-'; 
      newstr += accountNum[i].substring(7,10) + '-'; 
      newstr += accountNum[i].substring(10,14) + '-'; 
      newstr += accountNum[i].substring(14,20) + '-'; 
      newstr += accountNum[i].substring(20,24) + '-'; 
      newstr += accountNum[i].substring(24,28) + '-'; 
      newstr += accountNum[i].substring(28,31); 
      accountNum[i] = newstr; 
     } 
    } 
    accountNum.join(','); 
    $(this).val(accountNum); 

}); 

을 Jsfiddle에서 작동하지만 라이브 서식 문제에 관해서는 알 수 있습니다. 아래의 스크린 샷은 코드가 추가되거나 추가되지 않은 페이지를 보여줍니다. 이 코드는 두 코드베이스 간의 코드에서 유일한 변경 사항입니다. 중요하다면 Yii는 프레임 워크로 사용됩니다.

전에 before

after

+2

당신이 오류가있는 경우에 당신이 볼 수 콘솔 th at은 다른 스크립트의 실행을 막습니다. – svillamayor

+0

@svillamayor 콘솔에 내 문제 인 오류가 없습니다. – ComputerLocus

+2

나는 사람들에게 도움이되는 HTML 코드, 스크립트 등의 정보를 더 많이 제공해야한다고 생각한다. –

답변

1

jsfiddle.net/Lwv4U/25

JS 후 :

$('#Inventory_accountNumber').blur(function(){ 
    $(this).val(function(index, value){ 
    var str = value.replace(/[^\d]/g,''); 
    if (str.length < 10) return str; 
    return str.match(/(\d{4})?(\d{3})?(\d{3})?(\d{4})?(\d{6})?(\d{4})?(\d{4})?(\d+)?/) 
    .slice(1).join("-").replace(/\-+/g,'-'); 
    }); 
}); 
관련 문제