未被捕获的TypeError:无法读取null jqgrid的属性"replace" [英] Uncaught TypeError: Cannot read property 'replace' of null jqgrid

查看:150
本文介绍了未被捕获的TypeError:无法读取null jqgrid的属性"replace"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.当我尝试下面的功能时,除非列中没有空白单元格,否则它会很好地工作.如果单元格中有任何空白值,则它不起作用,然后整个页面变为空白.请帮我修复.

i am new to programming. when i try this below function, it works well unless there is a blank cell in the column. if there is any blank value in the cell then it is not working and then entire page goes blank. please help me to fix.

        function growth (cellvalue) {
                        var gcolor;
                        var numval=cellvalue
                        var val = Number(numval.replace("%",""));
                        if (val<0) {
                            gcolor = 'red';
                        } else if (val>0) {
                            gcolor = 'green';
                        } 
                        return '<span class="cellWithoutBackground" style="background-color:' + gcolor + ';">' + cellvalue + '</span>';
                    };

我也在下面尝试过此操作,例如if (val !== null && val<0)

i have also tried this below with not equal to null like this if (val !== null && val<0)

    function growth (cellvalue) {
var gcolor;
var numval=cellvalue
var val = Number(numval.replace("%",""));
if (val !== null && val<0) {
gcolor = 'red';
} else if (val !== null && val>0) {
gcolor = 'green';
} 
return '<span class="cellWithoutBackground" style="background-color:' + gcolor + ';">' + cellvalue + '</span>';
};

当没有空白单元格时,

都可以正常工作.但是当有空白单元格时,它将无法正常工作.请帮忙.

both works fine when there is no blank cell. but when there is a blank cell, it is not working. please help.

function growth (cellvalue) {
                        var numval=cellvalue
                        if(numval != null || numval != '' || numval != "")
                        {
                        var gcolor;
                        var val = Number(numval.replace("%",""));
                        if(val<0) {gcolor = 'red';}
                        else if(val >0) {gcolor = 'green';}
                        return '<span class="cellWithoutBackground" style="background-color:' + gcolor + ';">' + cellvalue + '</span>';
                        };
                       else{return '<span class="cellWithoutBackground" style="background-color:' + white + ';">' + cellvalue + '</span>';};

推荐答案

在尝试解析值之前,应先测试cellvalue != null .在JavaScript中测试cellvalue != null意味着与cellvalue !== null || cellvalue !== undefined相同.在这两种情况下,您都不应该使用cellvalue.replace(或numval.replace).

You should test cellvalue != null before trying to parse the value. Testing for cellvalue != null means in JavaScript the same as cellvalue !== null || cellvalue !== undefined. In both cases you should don't use cellvalue.replace (or numval.replace).

代码中的下一个可能的问题是将数字值用作输入数据.例如,您可以使用123而不是"123". Number类型没有方法replace,您可能会再遇到一个错误.我建议您使用String(cellvalue)将数字转换为字符串(如果还不是字符串的话).

The next possible problem in your code will be the usage of numeric values as input data. For example you can use 123 instead of "123". The Number type has no method replace and you could have one more error. I recommend you to use String(cellvalue) to convert the number to the string if it's not already the string.

尝试类似

function growth (cellvalue) {
    if (cellvalue == null) { // test for null or undefined
        return "";
    }
    cellvalue = Number(String(cellvalue).replace("%",""));
    return '<span class="cellWithoutBackground" style="background-color:' +
        (cellvalue < 0 ? 'red' : 'green') +
        ';">' + cellvalue + '</span>';
}

这篇关于未被捕获的TypeError:无法读取null jqgrid的属性"replace"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆