[英]Deselect a text box in jQuery after page reload if content is active
我有一個 Google Instant 樣式的 jQuery 搜索腳本,它查詢 PHP 文件,然后將結果解析為 HTML div。 當沒有查詢處於活動狀態時,會自動選擇文本框,但是當查詢處於活動狀態時,我希望它不會被自動選擇。 我知道這可以通過使用.blur();
function,但我怎樣才能讓它只使用.blur();
function 當查詢處於活動狀態並且頁面已重新加載時? 我希望每個人都能理解我的問題。
我的 jQuery 代碼是:
$(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 = 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();
}
});
在查看了消息來源后,我發現我的懷疑是正確的。 因為頁面上只有一個文本框,一旦你模糊了查詢一個,它就會回到它。 您可以在 html 頁面上創建另一個輸入節點,例如
<input type="text" style="display:none" id="invisible">
然后使用此代碼
if ($('#query').val().length) {
$('#invisible').focus();
}
那應該會讓你到達那里:)
啊哈。 我明白你的意思了。 您可以使用 JQuery 中的 .val() function 檢查文本框是否有任何內容,假設“#query”是文本框,您可以將該部分更改為
if($('#query').val().length == 0)
$('#query').focus();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.