简体   繁体   English

取消选择 jQuery 中的文本框

[英]Deselect a textbox in jQuery

I have a Google Instant style jQuery search script that queries a PHP file then parses the results into an HTML div.我有一个 Google Instant 样式的 jQuery 搜索脚本,它查询 PHP 文件,然后将结果解析为 HTML div。 It uses tabs for the user to define the search type that they want and when a search is made, a URL is created which is something like # type / query /.它使用选项卡让用户定义他们想要的搜索类型,当进行搜索时,会创建一个 URL,类似于 # type / query /。

Currently, the search box is selected on page load with the $('#query').focus();目前,在页面加载时使用$('#query').focus();选择搜索框function however I want it to be deselected when the page is reloaded and there is text in #query . function 但是我希望在重新加载页面并且#query中有文本时取消选择它。 How can I go about doing this?我怎么能 go 关于这样做? I hope you can understand my question.我希望你能理解我的问题。

My jQuery code is:我的 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();
    }
});

Remove $('#query').focus();删除$('#query').focus(); from where it is now, and add it in with an else like this:从它现在所在的位置,并将其添加到这样的 else 中:

if (window.location.hash.indexOf('#' + type + '/') == 0) {
    query = window.location.hash.replace('#' + type + '/', '').replace('/', '');
    $('#query').val(decodeURIComponent(query)).keyup();
}else {
    $('#query').focus();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM