Deselect a text box in jQuery after page reload if content is active
I have a Google Instant style jQuery search script that queries a PHP file then parses the results into an HTML div. When no query is active the text box is automatically selected but when a query is active I want it not to be automatically selected. I know this is possible to do by using the .blur();
function, but how can I make it only use the .blur();
function when a query is active and the page has been reloaded? I hope everyone can understand my question.
My jQuery code is:
$(document).ready(function () {
$('[id^=type_]').click(function () {
type = this.id.replace('type_', '');
$('[id^=type_]').removeClass('selected');
$('#type_' + type).addClass('selected');
return false;
});
$('#query').focus();
if (window.location.hash != "") {
var full 开发者_JAVA技巧= window.location.hash.replace('#', '');
var queryType = full.substring(0, full.indexOf("/"));
$('#type_' + queryType).click();
} else {
$('#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 Script';
$('#results').show();
if (query == '') {
window.location.hash = '';
document.title = 'My Search Script';
$('#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();
}
});
after reviewing the source I found that my suspicion was correct. Because there is only one text box on the page, once you blur the query one, it just goes back to it. You can make another input node on your html page e.g.
<input type="text" style="display:none" id="invisible">
and then use this code
if ($('#query').val().length) {
$('#invisible').focus();
}
and that should get you there :)
Ahha! I see what you mean. You can check if the textbox has anything in it with the .val() function in JQuery. Assuming that '#query' is the textbox, you can change that part to
if($('#query').val().length == 0)
$('#query').focus();
精彩评论