Javascript 增加可为null的int(int?)的值

Javascript 增加可为null的int(int?)的值,javascript,knockout.js,integer,nullable,Javascript,Knockout.js,Integer,Nullable,我使用了一个可为空的整数,并通过以下方式将其增加1: 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.q

我使用了一个可为空的整数,并通过以下方式将其增加1:

        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);

                    }

                }`
item.addItem=函数(){
//检查项目是否已在右侧的表中
if(viewModel.orderedItems.indexOf(this)<0){
如果(isNaN(this.qty)| | this.qty==“未定义”| | this.qty替换:

this.qty + 1;

但您有一个问题,因为某些内容更改了数量值的类型。

替换:

this.qty + 1;


但您遇到了一个问题,因为某些内容更改了数量值的类型。

您可以尝试使用将此字符串转换为数字,然后向其中添加1

parseInt(this.qty,10) + 1;
其中10是基数

你也可以使用

除了一个广泛使用的构造之外,还有一个尝试将其转换为数字的构造

因此,在你的情况下,它将是

+this.qty + 1;

您可以尝试使用将此字符串转换为数字,然后向其中添加1

parseInt(this.qty,10) + 1;
其中10是基数

你也可以使用

除了一个广泛使用的构造之外,还有一个尝试将其转换为数字的构造

因此,在你的情况下,它将是

+this.qty + 1;

Perfecto!(10分钟内可接受)Perfecto!(10分钟内可接受)这也行得通,谢谢你的快速回复!@user2181397:你不需要为这个例子指定基数,对吗?它会自动将它检测为十进制。@niandrei我个人不这么认为,但是我添加基数只是为了安全。这也行得通,谢谢你的快速回复!@user2181397:你不需要指定为本例指定基数,对吗?它会自动检测为十进制。@niandrei我个人不这么认为,但是我添加基数只是为了安全。您也可以使用
+this.qty+1
强制转换类型,虽然可读性较差!您也可以使用
+this.qty+1
强制转换类型,尽管可读性较差!