2011-07-10 13 views
0

PHPファイルの結果をHTML divに解析するjQuery検索スクリプトがあります。私はアクティブなクエリがないときに検索ボックスを自動的に選択し、アクティブなクエリがあるときは選択しないようにしたい。これどうやってするの?あなたが私の質問を理解できることを願っています。jQueryでテキストボックスが自動的に選択される

私の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(); 
    } 
}); 

答えて

1

このAjaxの編集が適切に無効にして、

$.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 /有効クエリの中に#queryフィールドを集中します)

if($('#results').html() == '') 
{ 
    $('#query').focus(); 
} 
+0

あなたのコメントリクエストごとに$(document).ready機能の – AlienWebguy

関連する問題