简体   繁体   English

检查 null 或 jQuery 中的未定义值

[英]Check null or undefined value in jQuery

I'm really getting crazy with this issue...and even I've read tons of topics I can't figure it out...so sorry in advance for the already discussed topic but I need a specific help.我真的对这个问题感到疯狂......即使我已经阅读了大量我无法弄清楚的主题......对于已经讨论过的主题,我提前感到抱歉,但我需要一个具体的帮助。

What I need to do: check if a variable is not empty;我需要做的:检查变量是否为空; in this case print a badge.在这种情况下打印徽章。

The remote framework is Codeigniter 3, I'm sure about the code, the server script return both an empty variable (infos) and an undefined variable (crid) in JSON format.远程框架是 Codeigniter 3,我确定代码,服务器脚本返回 JSON 格式的空变量(infos)和未定义变量(crid)。

The literal template in Jquery to print the badges is the following: Jquery 中用于打印徽章的文字模板如下:

$(`
    
    <p class="badges">
    ${(() => {
      if (data.pictures[i].status == '1'){
        return `<span class="badge badge-success">Published</span>`
      } else {
        return `<span class="badge badge-warning">Hidden</span>`
      }
    })()}
    ${(() => {
        if (data.pictures[i].infos){
            console.log('Infos are ' + data.pictures[i].infos);
            return `<span class="badge badge-info">Info</span>`
        }
    })()}
    ${(() => {
        if (typeof data.pictures[i].crid !== 'undefined' && data.pictures[i].crid !== null){
            return `<span class="badge badge-primary">Credit</span>`
        }
    })()}
    </p>
`)

The first conditional code works fine ( data.pictures[i].status), the second and third ones are getting me crazy.第一个条件代码工作正常(data.pictures[i].status),第二个和第三个让我抓狂。 The second conditional code ( data.pictures[i].infos ) return undefined in browser...the variable doesn't exists in JSON code returned by the server script.第二个条件代码( data.pictures[i].infos )在浏览器中返回 undefined ...服务器脚本返回的 JSON 代码中不存在该变量。 The third conditional code ( data.pictures[i].crid ) return undefined in browser... the variable in JSON data exists but is empty.第三个条件代码( data.pictures[i].crid )在浏览器中返回 undefined ... JSON 数据中的变量存在但为空。

I want to get rid of undefined in browser if the variable is empty or not exists....in case it has a value I want to print the badge.如果变量为空或不存在,我想摆脱浏览器中的未定义......如果它有一个值,我想打印徽章。

Of course I did endless test replacing the conditional code...but sometimes I get the badge even if the JSON variable is empty...otherwise I get undefined.当然,我做了无休止的测试来替换条件代码……但有时即使 JSON 变量为空,我也会得到徽章……否则我会得到未定义的。

Thanks a lot for any help or hint非常感谢任何帮助或提示

Your functions are just an if and return nothing if they do not go into the if.您的函数只是一个 if,如果它们不 go 进入 if,则不会返回任何内容。 So that means it returns undefined .所以这意味着它返回undefined

You would need to return '' from the functions.您需要从函数中return ''

${(() => {
    if (data.pictures[i].infos){
        console.log('Infos are ' + data.pictures[i].infos);
        return `<span class="badge badge-info">Info</span>`
    }
    return '';
})()}

Personally I would just use a ternary就个人而言,我只会使用三元

const status = data.pictures[i].status == '1' ? 
    '<span class="badge badge-success">Published</span>' : 
    '<span class="badge badge-warning">Hidden</span>'
const info = data.pictures[i].infos ? 
    '<span class="badge badge-info">Info</span>' : ''
const crid = (typeof data.pictures[i].crid !== 'undefined' && data.pictures[i].crid !== null) ? 
    '<span class="badge badge-primary">Credit</span>' : ''

$(`<p class="badges">
    ${status}
    ${info}
    ${crid}
</p>`)

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

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