[英]Automatically call a click handler to load a jQuery dialog box on page load
[英]Text box on page load automatically selected with jQuery
我有一个 jQuery 搜索脚本,它将 PHP 文件的结果解析为 HTML div。 我希望在没有查询活动时自动选择搜索框,并且在有查询活动时不选择它。 我怎样才能做到这一点? 我希望你能理解我的问题。
我的 jQuery 搜索脚本是:
$(document).ready(function(){
$('[id^=type_]').click(function(){
type=this.id.replace('type_','');
$('[id^=type_]').removeClass('selected');
$('#type_'+type).addClass('selected');
return false;
});
$('#type_search').click();
$('#query').keyup(function(){
var query=$(this).val();
var url='/'+type+'/'+query+'/';
window.location.hash=''+type+'/'+query+'/';
document.title=$(this).val()+' - My Search';
$('#results').show();
if(query==''){
window.location.hash='';
document.title='My Search';
$('#results').hide();
}
$.ajax({
type:'GET',
url:url,
dataType:'html',
success:function(results){
$('#results').html(results);
}
});
});
if(window.location.hash.indexOf('#'+type+'/')==0){
query=window.location.hash.replace('#'+type+'/','').replace('/','');
$('#query').val(decodeURIComponent(query)).keyup();
}
});
此 ajax 编辑将在查询期间正确禁用和启用/聚焦您的#query 字段
$.ajax({
type:'GET',
url:url,
dataType:'html',
beforeSend:function(){
$('#query').prop('disabled',true);
},
success:function(results){
$('#results').html(results);
$('#query').prop('disabled',false).focus();
}
});
编辑:根据您的评论请求将此添加到$(document).ready
function 的底部(内部):
if($('#results').html() == '')
{
$('#query').focus();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.