繁体   English   中英

使用带有条件的jquery重新加载页面

[英]reloading page with jquery with conditional

我有这个代码(jQuery)。 基本上我想重新加载页面,如果有#SchoolName存在的输入,但它错过了class="ac_input" ,如果该类存在,请不要重新加载页面。 这可能吗?

<input type="text" onfocus="showVal(this.value);" value="" id="SchoolName" size="30" maxlength="50" name="SchoolName" autocomplete="off" class="ac_input">

function autocomplete() {
         $("#SchoolName").autocomplete("ajaxFuncs.php",{cacheLength:1,mustMatch:1,extraParams:{getSchoolName:1}});
    };

$(document).ready(function(){
    setTimeout("autocomplete()", 500);
    // what do i have to add here???
});

尝试这个

$(document).ready(function(){
    setTimeout(function(){
         autocomplete()
    }, 500);

    // what do i have to add here???
    if($('#SchoolName').length && !$('#SchoolName').hasClass('ac_input')){
        console.log('reload');
        location.reload();
    }

});

根据您的要求,我们可以在纯JavaScript中编写上述代码,如下所示。

window.onload = function(){
    setTimeout(function(){
        autocomplete()
    }, 500);

    if(document.getElementById('SchoolName') 
       && document.getElementById('SchoolName').className != 'ac_input'){
        console.log('reload');
        location.reload();
    }
}
if (!$('#SchoolName').is('.ac_input'))
    window.location.reload();

使用:not()选择器

$("#SchoolName:not(.ac_input)").autocomplete("ajaxFuncs.php",{cacheLength:1,mustMatch:1,extraParams:{getSchoolName:1}});

暂无
暂无

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

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