2012-08-13 69 views
0

我需要將貨幣輸出添加到jQuery函數。因此,像1577334.668這樣的數字將打印爲1,577,335美元。我以爲我應該在控制器中這樣做,但我的老闆今天告訴我,我應該在jQuery視圖中執行此任務,我只是不知道該怎麼做。我已經在下面包含了jQuery代碼,並且在需要打印貨幣金額的變量旁邊添加了註釋。該變量被稱爲SC[i]。任何意見或建議將不勝感激!謝謝你的幫助!如何將貨幣輸出添加到jQuery函數

//Spend Category function for monthly 
     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 = ''; 

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

         months_html += 
              '<div id="SC-' + index + '-' + months[i] + '" class="month-wrapper tree border-white">' + 
              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).show('slide', { direction: 'up' }, 'fast'); 

     }; 
+0

你有沒有嘗試什麼嗎?你遇到什麼問題? – 2012-08-13 18:35:35

+0

是的,我一直在搞它大約兩個小時,我不能使用{0:C}的方式,我會在控制器中做一個數組? – Goldentp 2012-08-13 18:37:17

+1

變量'SC [i]'是否已經使用逗號進行格式化? – 2012-08-13 18:37:36

回答

1

這個jQuery插件將幫助您:Format Currency

添加一個類來這條線(例如貨幣)

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

然後$(".currency").formatCurrency();會做的伎倆。

頁也有一些演示,可以幫助您開始

+0

謝謝@ClydeLobo,那很棒! – Goldentp 2012-08-13 21:30:51

0

其他插件,可能會更容易爲C#鄉親用的是Globalize。樣本和解釋可在Readme中找到。

樣品:

Globalize.format(1234.567, "n"); // "1,234.57"