3

次のコードを追加すると、ページの書式設定が正しくありません。

$('#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 は、使用中のこのコードを示しています: http://jsfiddle.net/Lwv4U/14/

ご覧のとおり、jsfiddle では機能しますが、ライブ フォーマットの問題が存在します。以下のスクリーンショットは、コードが追加されたページと追加されていないページを示しています。このコードは、2 つのコード ベース間のコードの唯一の変更点です。問題があれば、Yii がフレームワークとして使用されています。

前

後

4

1 に答える 1

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,'-');
  });
});
于 2013-08-21T19:48:36.497 に答える