2012-01-17 76 views
0

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

<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??? 
}); 
+2

不要传递一个字符串'setTimeout'。 – SLaks 2012-01-17 18:26:59

回答

2

试试这个

$(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(); 
    } 
} 
+0

重新加载不会发生:S从控制台没有错误:S – Drazek 2012-01-17 18:48:12

+0

可能是条件不满意,所以重新加载没有发生。我在控制台中放了一条日志消息,可以检查它吗? – ShankarSangoli 2012-01-17 19:23:40

+0

在日志中没有任何东西,意味着代码永远不会执行:S我不明白为什么不:S 什么是正常的Javascript代码,而不是jQuery? – Drazek 2012-01-17 20:01:29

0
if (!$('#SchoolName').is('.ac_input')) 
    window.location.reload(); 
+0

如果'#SchoolName'在页面上不存在,这将返回'true'。 – ShankarSangoli 2012-01-17 18:36:32

0

使用:not()选择

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