淘汰赛js和全球化

发布于 2024-12-05 02:04:48 字数 422 浏览 1 评论 0原文

我不知道如何使用淘汰赛 js 和非美国语言环境来处理计算。我的逗号是 , 并且使用 jquery.globalization 插件正确验证,但淘汰计算给我一个 NaN。 knockout js 是否以任何方式支持这一点,或者是否存在任何解决方法?

示例:

使 knockout js 站点上的 cartEditor 示例正常工作,允许数量字段中使用小数值,并允许全球化输入(作为逗号)和输出格式

http://knockoutjs.com/examples/cartEditor.html

我需要它在 asp.net mvc 3 网站上工作,因为我正在使用nb-NO 文化和模型绑定器期望 , 作为逗号符号

I can't figure out how to handle calculations using knockout js and a non us locale. My comma is , and is validated correctly using the jquery.globalization plugin but the knockout calculation is giving me a NaN. Does knockout js support this in any way or does it exist any workarounds?

Example:

Make the cartEditor example on the knockout js site work allowing decimal values in the quantity field and allowing globalized input (, as comma sign) and output formatting

http://knockoutjs.com/examples/cartEditor.html

I need this to work on a asp.net mvc 3 site because I am running the site using the nb-NO culture and the model binder is expecting , as the comma sign

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蒗幽 2024-12-12 02:04:48

我通过编写一个自定义绑定来完成类似的操作,该绑定包装 autoNumeric.js 进行格式化。 (gist)

ko.bindingHandlers.autoNumeric = function ($) {

    function getElementValue(el) {
        return parseFloat(el.autoNumericGet(), 10);
    }

    function getModelValue(accessor) {
        return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
    }

    return {
        init: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                bindings = bindingsAccessor(),
                settings = bindings.settings,
                value = valueAccessor();

            function updateModelValue() {
                value(getElementValue($el));
            };

            $el.autoNumeric(settings);
            $el.autoNumericSet(getModelValue(value), settings);
            $el.change(updateModelValue);
        },
        update: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                newValue = getModelValue(valueAccessor()),
                elementValue = getElementValue($el),
                valueHasChanged = (newValue != elementValue);

            if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) {
                valueHasChanged = true;
            }

            if (valueHasChanged) {
                $el.autoNumericSet(newValue);
                setTimeout(function () { $el.change() }, 0);
            }
        }
    };
}

使用此自定义 autoNumeric 绑定的数据绑定如下所示:

<input data-bind="autoNumeric:amount, settings:{aSign:'

查看 autoNumeric.js 的扩展选项格式化以查看您可以使用这些设置执行哪些操作。

}" />

查看 autoNumeric.js 的扩展选项格式化以查看您可以使用这些设置执行哪些操作。

I did something like this by writing a custom binding that wrapped autoNumeric.js for the formatting. (gist)

ko.bindingHandlers.autoNumeric = function ($) {

    function getElementValue(el) {
        return parseFloat(el.autoNumericGet(), 10);
    }

    function getModelValue(accessor) {
        return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
    }

    return {
        init: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                bindings = bindingsAccessor(),
                settings = bindings.settings,
                value = valueAccessor();

            function updateModelValue() {
                value(getElementValue($el));
            };

            $el.autoNumeric(settings);
            $el.autoNumericSet(getModelValue(value), settings);
            $el.change(updateModelValue);
        },
        update: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                newValue = getModelValue(valueAccessor()),
                elementValue = getElementValue($el),
                valueHasChanged = (newValue != elementValue);

            if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) {
                valueHasChanged = true;
            }

            if (valueHasChanged) {
                $el.autoNumericSet(newValue);
                setTimeout(function () { $el.change() }, 0);
            }
        }
    };
}

the data binding using this custom autoNumeric binding looks like this:

<input data-bind="autoNumeric:amount, settings:{aSign:'

Check out autoNumeric.js extensive options for formatting to see what you can do ewith the settings.

}" />

Check out autoNumeric.js extensive options for formatting to see what you can do ewith the settings.

顾铮苏瑾 2024-12-12 02:04:48

修改为兼容最新的autoNumeric版本(1.9.x)

(function($) {

    function getElementValue(el) {
        return parseFloat(el.autoNumeric('get'), 10);
    }

    function getModelValue(accessor) {
        return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
    }

    ko.bindingHandlers.autoNumeric = {
        init: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                bindings = bindingsAccessor(),
                settings = bindings.settings,
                value = valueAccessor();

            function updateModelValue() {
                value(getElementValue($el));
            };

            if (settings.pSign === 's') {
                settings.aSign = ' ' + settings.aSign;
            } else {
                settings.aSign = settings.aSign + ' ';
            }

            $el.autoNumeric(settings);
            $el.autoNumeric('set', getModelValue(value));
            $el.change(updateModelValue);
        },
        update: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                newValue = getModelValue(valueAccessor()),
                elementValue = getElementValue($el),
                valueHasChanged = (newValue != elementValue);

            if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) {
                valueHasChanged = true;
            }

            if (valueHasChanged) {
                $el.autoNumeric('set', newValue);
                setTimeout(function () { $el.change() }, 0);
            }
        }
    };
})(jQuery);

Modified it to make it compatible with the latest autoNumeric version (1.9.x)

(function($) {

    function getElementValue(el) {
        return parseFloat(el.autoNumeric('get'), 10);
    }

    function getModelValue(accessor) {
        return parseFloat(ko.utils.unwrapObservable(accessor()), 10);
    }

    ko.bindingHandlers.autoNumeric = {
        init: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                bindings = bindingsAccessor(),
                settings = bindings.settings,
                value = valueAccessor();

            function updateModelValue() {
                value(getElementValue($el));
            };

            if (settings.pSign === 's') {
                settings.aSign = ' ' + settings.aSign;
            } else {
                settings.aSign = settings.aSign + ' ';
            }

            $el.autoNumeric(settings);
            $el.autoNumeric('set', getModelValue(value));
            $el.change(updateModelValue);
        },
        update: function (el, valueAccessor, bindingsAccessor, viewModel) {
            var $el = $(el),
                newValue = getModelValue(valueAccessor()),
                elementValue = getElementValue($el),
                valueHasChanged = (newValue != elementValue);

            if ((newValue === 0) && (elementValue !== 0) && (elementValue !== "0")) {
                valueHasChanged = true;
            }

            if (valueHasChanged) {
                $el.autoNumeric('set', newValue);
                setTimeout(function () { $el.change() }, 0);
            }
        }
    };
})(jQuery);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文