0

所以我使用名为 FormatCurrency 的 Jquery 插件(这应该很明显)以货币格式格式化一些数字。jQuery 函数对第一行数字非常有效,但第二行存在一些问题。

让我先解释一下我在做什么:

如果我单击树并展开我要选择的树的行,则会打开另外两行资本和费用。jQuery 在大写行上运行良好,使用格式并且行从 123456 更改为 $123,456,但费用行保持不变。

我还发现如果我点击不同的区域,Expense 行确实接受 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');

        };
4

1 回答 1

1

在将所有元素添加到 DOM.formatCurrency 调用 to 。

例如,您可以像这样重构:

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

于 2012-08-23T20:27:41.713 回答