使Jquery var只在字段被隐藏时起作用

Making Jquery var to only work if the field is hidden

本文关键字:隐藏 起作用 字段 Jquery var      更新时间:2023-09-26

我想让一个Jquery变量只在页面隐藏字段时工作。

在我的例子中,我有一个由PHP生成的页面。如果用户通过目标页面访问该页,则侧边栏上的目标选择隐藏在这个php生成的页面中。用户访问该页面的另一种方式是点击列表链接,该链接将显示所有输入字段,以便他们可以进行选择。

我希望我的location_bar变量只在目标输入字段被隐藏时访问页面时才工作。当他们通过列表链接访问时就不会…

这是我的变量,if else语句只与该变量相关。

var location_bar = $("hidden input[name='destinations[]']:hidden").val();
if(location_bar <= 0){ ...........
 ........
}else{
    jQuery.ajax({
        type: 'GET',
        url: 'http://www.pgtpackages.com/api_courselist.php?page='+ nextPage +'&format=html&destinations%5B%5D='+ location_bar +'',
        success: function(html) {
            is_loaded = true;
            nextPage++;
            if(html){
                jQuery("#infiscroll").append(html);
                jQuery('div#loadMore').hide();
            }else{
                jQuery('div#loadMore').replaceWith("<center><h1  style='color:red'>End of Content !!!!!!!</h1></center>");
            }
        }
    });
}

我跳过了所有无关的代码。此外,当我console.log location_bar上的列表链接,我得到一个随机值,我没有选择。

您可以检查结果是否为undefined,例如:

var location_bar = $("hidden input[name='destinations[]']:hidden").val();
if(location_bar != undefined)
{
    jQuery.ajax({
        type: 'GET',
        url: 'http://www.pgtpackages.com/api_courselist.php?page='+ nextPage +'&format=html&destinations%5B%5D='+ location_bar +'',
        success: function(html) {
            is_loaded = true;
            nextPage++;
            if(html){
                jQuery("#infiscroll").append(html);
                jQuery('div#loadMore').hide();
            }else{
                jQuery('div#loadMore').replaceWith("<center><h1  style='color:red'>End of Content !!!!!!!</h1></center>");
            }
        }
    });
}