开发者

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. 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/.

Currently, the search box is selected on page load with the $('#query').focus(); function however I want it to be deselected when the page is reloaded and there is text in #query. How can I go about doing this? I hope you 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 = 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: 开发者_JAVA技巧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(); from where it is now, and add it in with an else like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜