增加可为空的 int(int?)的值

Increasing the value of a nullable int (int?)

我正在使用可为空的整数,并按以下方式将其增加一:

        item.addItem = function () {
                //Check if item is already in the table on the right
                if (viewModel.orderedItems.indexOf(this) < 0) {

                    if (isNaN(this.qty) || this.qty == 'undefined' || this.qty <= 0) {
                        //alert('is NaN1')

                        this.qty = 1;
                        //this.qty.value = 0;
                    }
                    //alert(this.qty.value);
                    viewModel.orderedItems.push(this);
                }
                else {
                    if (isNaN(this.qty) || this.qty == 'undefined' || this.qty <= 0) {
                        //alert('is NaN2')
                        this.qty + 1;
                        //this.qty.value = 0;
                    }
                    else {
                        viewModel.orderedItems.remove(this);
                        this.qty = this.qty + 1;
                        viewModel.orderedItems.push(this);

                    }

                }`

现在,此数量显示在输入字段中,您可以在此处更改它。

比方说,我写了 15 作为数量,并决定 'click' 将它增加一,结果是 '151' (15 +1),而不是 16。 我怎样才能让它实际增加一,而不是加一?

感谢您的意见。

替换:

this.qty + 1;

来自

parseInt(this.qty) + 1;

但是你遇到了问题,因为某些东西改变了数量值的类型。

您可以尝试使用 parseInt 将此字符串转换为数字并向其加 1

parseInt(this.qty,10) + 1;

其中 10 是基数

您也可以使用Number

除了一个广泛使用的构造之外,使用 Unary_plus 试图将其转换为数字 .

所以在你的情况下它将是

+this.qty + 1;