如何在jquery中检查null值而不会出现错误? [英] How can I check a value for null in jquery without getting an error?

查看:47
本文介绍了如何在jquery中检查null值而不会出现错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:



I've got this code:

if (returneddata.daterangeparams.TimeUnitsFrom != null) {





...当值确实为null时抛出此错误(如Chrome开发工具控制台中所示):



索引:1031未捕获TypeError:无法读取属性'TimeUnitsFrom 'null



那么如何以一种可以避免错误的方式检查null?



根据http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript的建议,我甚至尝试过这个:





...which throws this error (as seen in the Chrome Dev Tools console) when the value is, indeed, null:

Index:1031 Uncaught TypeError: Cannot read property 'TimeUnitsFrom' of null

So how can I check for null in a way that I can avoid the error?

Based on a suggestion here http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript, I even tried this:

if (returneddata.daterangeparams.TimeUnitsFrom != null && variable !== undefined) {





...但我仍然得到sa我从浏览器的内心发出的声音。



代码在更大的背景下:





...but I still get the same whinging from the guts of the browser.

Code in greater context:

function populatedaterangeprams(rptval, returneddata) {
                    var fromval = '';
                    var toval = '';
                    if (returneddata.daterangeparams.TimeUnitsFrom != null && 

returneddata.daterangeparams.TimeUnitsFrom !== undefined) {
                        fromval = returneddata.daterangeparams.TimeUnitsFrom;
                    }
                    if (returneddata.daterangeparams.TimeUnitsTo != null && 

returneddata.daterangeparams.TimeUnitsTo !== undefined) {
                        toval = returneddata.daterangeparams.TimeUnitsTo;
                    }

                    if (rptval === 1) {
                        // Produce Usage
                        $("#produsagefrom").val(fromval);
                        $("#produsageto").val(toval);
                    } else if (rptval === 2) {
			. . .





那我怎样才能安全地检查空?



< b>我尝试了什么:



基于此处的建议http://stackoverflow.com/questions/12271750/how-to-check- if-a-variable-are-both-null-and-undefined-in-javascript,我甚至试过这个:





So how can I safely check for null?

What I have tried:

Based on a suggestion here http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript, I even tried this:

if (returneddata.daterangeparams.TimeUnitsFrom != null && variable !== undefined) {





...但我仍然从内心得到同样的抱怨浏览器。



...but I still get the same whinging from the guts of the browser.

推荐答案

#produsagefrom )。val(fromval);
("#produsagefrom").val(fromval);


#produsageto )VAL(toval)。
} else if (rptval === 2 ){
。 。 。
("#produsageto").val(toval); } else if (rptval === 2) { . . .





那我怎样才能安全地检查空?



< b>我尝试了什么:



基于此处的建议http://stackoverflow.com/questions/12271750/how-to-check- if-a-variable-are-both-null-and-undefined-in-javascript,我甚至试过这个:





So how can I safely check for null?

What I have tried:

Based on a suggestion here http://stackoverflow.com/questions/12271750/how-to-check-if-a-variable-is-both-null-and-or-undefined-in-javascript, I even tried this:

if (returneddata.daterangeparams.TimeUnitsFrom != null && variable !== undefined) {





...但我仍然从内心得到同样的抱怨浏览器。



...but I still get the same whinging from the guts of the browser.


你需要检查每一步,看看它是否 null

You need to check each step to see if it's null:
if (returneddata != null && returneddata.daterangeparams != null && returneddata.daterangeparams.TimeUnitsFrom != null) {


这篇关于如何在jquery中检查null值而不会出现错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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