[英]Text box on page load automatically selected with jQuery
I have a jQuery search script that parses results from a PHP file into an HTML div.我有一个 jQuery 搜索脚本,它将 PHP 文件的结果解析为 HTML div。 I want the search box to be selected automatically when there is no query active and for it not to be selected when there is a query active.我希望在没有查询活动时自动选择搜索框,并且在有查询活动时不选择它。 How can I do this?我怎样才能做到这一点? I hope you can understand my question.我希望你能理解我的问题。
My jQuery search script is:我的 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();
}
});
This ajax edit will properly disable and enable/focus your #query field during the query此 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();
}
});
EDIT: Add this to the bottom (inside) of your $(document).ready
function per your comment request:编辑:根据您的评论请求将此添加到$(document).ready
function 的底部(内部):
if($('#results').html() == '')
{
$('#query').focus();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.