简体   繁体   English

想在jQuery中检查null值

[英]want to check value of null in jquery

$('#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 行在控制台中显示以下错误

Uncaught ReferenceError: NULL is not defined 未捕获的ReferenceError:未定义NULL

NULL and null are two different things and you're most probably looking to check against the latter. NULLnull是两个不同的事物,您很可能希望检查后者。 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 除此之外,更好的做法是测试结果数组的长度是否大于1 ,因此请尝试替换

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

with

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

If separator is not found or is omitted, the array contains one element consisting of the entire string. 如果找不到或省略了分隔符,则数组包含一个由整个字符串组成的元素。

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

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM