jQuery 检查空变量

ste*_*Kim 2 jquery

所以,我有以下 js 标记:

jQuery(document).on( 'click', '.rhpcon', function(e) {  
    var rhp_name = jQuery(this).find('.rhi').data("name");

    var my_html = '<div class="rhfi">'+ rhp_name +'</div>'; 
        my_html += '<div class="something">Show something else</div>';                          
    jQuery('.rh_pop').html(my_html);                        
});
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,该rhp_name变量在硬编码的 html 中使用。

然而,有些情况data-name不存在(即没有rhp_name变量)。

在没有rhp_name变量的情况下,我不想my_html使用第一行。

如何检查是否var为空等?

谢谢!

小智 9

if(typeof rhp_name !== 'undefined' && rhp_name != ''){
Run Code Online (Sandbox Code Playgroud)

应该能解决问题