2012-08-23 56 views
0

所以我使用名爲FormatCurrency的Jquery插件(這應該是顯而易見的)格式化貨幣格式的一些數字。 jQuery函數適用於第一行數字,但第二行有一些問題。Jquery插件FormatCurrency不能正常工作

讓我解釋一下我做的第一:

如果我點擊樹和擴大我要選擇樹開闢了兩個行投資和運營的行。 jQuery在大寫行上效果很好,使用了格式,並將行從123456更改爲123,456美元,但費用行保持不變。

我還發現,如果我點擊不同的區域,如果我用另一個數字展開另一行,費用行也會接受formatCurrency樣式的更改。所以Jquery正在工作,它只是沒有完成風格變化,直到我點擊另一行,如果我回到我最初點擊的原始行上,樣式變化僅在費用行上被刪除。我希望這一切都有道理。我已經在下面發佈了我的代碼,我會經常檢查回來,隨時提出任何可能有助於解決問題的問題。感謝您的幫助!

pa_click = function (pa_label) { 
      PA_ID = pa_label.getAttribute('pa_id'); 

      var pa_details = document.getElementById('pa-details-' + PA_ID); 

      jQuery.getJSON('@Url.Action("getAjaxSCs")', { PA: pa_label.title }, function (SCS) { 
       pa_details.innerHTML = ""; 
       jQuery.each(SCS, function (index, SC) { 

        months_html = ''; 
        jQuery('.currency').formatCurrency({ 
         colorize: true, 
         roundToDecimalPlace: -2, 


        }); 

        for (var i = 0; i < 12; i++) { 

         index = index.replace(/\s/g, "-"); 
         months_html += 
              '<div id="SC-' + index + '-' + months[i] + '" class="month-wrapper tree border-white currency">' + // This is where I add the currency class 
              SC[i] + // This is the variable I need to replace with code to add currency to the amount 
              '</div>'; 
        } 

        pa_details.innerHTML += 

          '<div id ="Spend-Category-' + index + '" class="sc-wrapper tree border">' + 
           '<div id ="sc-title-' + index + '" class="sc-title">' + 
            '<div class = "sc-label" title = "' + index + '" SC_id="' + index + '" onclick = "sc_click(this)">' + index + '</div>' + 
            months_html + 
           '</div>' + 
           '<div id="sc-details-' + index + '" class = "pa-details" style = "display:none">' + index + '</div>' + 
          '</div>'; 
       }) 
      }); 
      jQuery('#pa-details-' + PA_ID).toggle('fast'); 

     }; 

回答

1

將調用後,你已經添加了所有元素的DOM .formatCurrency

例如,你可以重構像這樣:

pa_details.innerHTML = ""; 

jQuery.each(SCS, function (index, SC) { 
    months_html = ''; 
    for (var i = 0; i < 12; i++) { 
    ... 
    } 
    pa_details.innerHTML += "BIG CONCATENATION HERE"; 
}); 

jQuery('.currency').formatCurrency({ 
    colorize: true, 
    roundToDecimalPlace: -2 
}); 

這可以讓你嘗試元素格式化之前的所有元素存在於DOM。

+0

不客氣! – BradBrening

+0

感謝布拉德,我認爲在我的頭後,我有一種感覺,就是這樣的感覺。感謝您的回答,它提供了豐富而簡明的信息,並且非常有幫助,因爲它可以立即生效,而且不需要對代碼進行其他更改 – Goldentp