我有这段代码(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();
}
}
关于javascript - 使用jquery和条件重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899698/