想在jquery中检查null的值

时间:2022-01-28 17:07:06
$('#test').click(function() {
    var modal_class = $("#test").attr('data-code');
    $('.sign').each(function() {
        var sign = $(this).attr("href");
        if (sign.split("&")[0] != NULL) {
            var sign1 = sign.split("&")[0];
        } else {
            sign1 = $(this).attr("href");
        }
        $(this).attr("href", sign1 + "&test_carrer=" + modal_class);
    });
});

Now these sign and sign1 variables both are href I want to check value of sign.split("&")[0] which shows error NULL is not defined.. Above code is to check a variable on which basis url is generated.

现在这些sign和sign1变量都是href我想检查sign.split(“&”)[0]的值,它显示错误NULL未定义。上面的代码是检查生成基于url的变量。

if(sign.split("&")[0] != NULL){  

line is showing the following error in console

line在控制台中显示以下错误

Uncaught ReferenceError: NULL is not defined

未捕获的ReferenceError:未定义NULL

2 个解决方案

#1


0  

Try any of these..

试试其中任何一个......

<script>       
$('#test').click(function(){
 var modal_class = $("#test").attr('data-code');
 $('.sign').each(function() {
 var sign = $(this).attr("href");
 if(sign.split("&")[0] !== null)
 //OR
 if(sign.split("&")[0]) // This will work for null, 0, false
 //OR
 if(sign.split("&")[0] != null)
 { 
  var sign1=sign.split("&")[0]; 
   }else{
  sign1= $(this).attr("href");
  }
    $(this).attr("href", sign1+"&test_carrer="+modal_class);
 });
 });
 </script>

#2


0  

NULL and null are two different things and you're most probably looking to check against the latter. That thing aside, a better practice would be to test if the length of the resulting array is larger than 1, so try to replace

NULL和null是两个不同的东西,你很可能希望检查后者。除此之外,更好的做法是测试结果数组的长度是否大于1,因此请尝试替换

if(sign.split("&")[0] != NULL) {...}

if(sign.split(“&”)[0]!= NULL){...}

with

if (sign.split("&").length > 1) {...}

if(sign.split(“&”)。length> 1){...}

If separator is not found or is omitted, the array contains one element consisting of the entire string.

如果未找到separator或省略该分隔符,则该数组包含一个由整个字符串组成的元素。

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

#1


0  

Try any of these..

试试其中任何一个......

<script>       
$('#test').click(function(){
 var modal_class = $("#test").attr('data-code');
 $('.sign').each(function() {
 var sign = $(this).attr("href");
 if(sign.split("&")[0] !== null)
 //OR
 if(sign.split("&")[0]) // This will work for null, 0, false
 //OR
 if(sign.split("&")[0] != null)
 { 
  var sign1=sign.split("&")[0]; 
   }else{
  sign1= $(this).attr("href");
  }
    $(this).attr("href", sign1+"&test_carrer="+modal_class);
 });
 });
 </script>

#2


0  

NULL and null are two different things and you're most probably looking to check against the latter. That thing aside, a better practice would be to test if the length of the resulting array is larger than 1, so try to replace

NULL和null是两个不同的东西,你很可能希望检查后者。除此之外,更好的做法是测试结果数组的长度是否大于1,因此请尝试替换

if(sign.split("&")[0] != NULL) {...}

if(sign.split(“&”)[0]!= NULL){...}

with

if (sign.split("&").length > 1) {...}

if(sign.split(“&”)。length> 1){...}

If separator is not found or is omitted, the array contains one element consisting of the entire string.

如果未找到separator或省略该分隔符,则该数组包含一个由整个字符串组成的元素。

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split