提案された結果のドロップダウンリストを表示するフィールドに検索クエリを入力できる基本的な検索機能があります。提案された結果のいずれかをクリックすると、そのレコード (MySQL データベースに保存されている) の値が意図したとおりにフィールドに挿入されます。ただし、スクリプトを最初に実行した直後に同じことをしようとすると、うまくいきません。しかし、ページをリロードすると、再び機能します。つまり、スクリプトを初めて実行したときは機能しますが、ページをリロードしない限り、スクリプトの後続の実行では機能しません。スクリプトを実行すると、最初の実行後に「自動的にオフ」になり、スクリプトを再度実行できなくなります。何か案は?コードは次のとおりです。
<script>
$(function(){
var index = -1;
$('#myID').keyup(function(e){
if (e.keyCode == 38){
index = (index == 0) ? 0 : index - 1;
$('tr.myRow').removeClass('gray');
$('tr.myRow:eq(' + index + ')').addClass('gray');
return false;
}
else if (e.keyCode == 40){
index = (index + 1 >= $('tr.myRow').length) ? $('tr.myRow').length - 1 : index + 1;
$('tr.myRow').removeClass('gray');
$('tr.myRow:eq(' + index + ')').addClass('gray');
return false;
}
else
{
var str = $('#myID').val();
mySearch(str);
}
index = -1;
});
});
</script>
<script>
$(function(){
$('#myID').keydown(function(e){
if (e.keyCode == 13){
var functionName = $('#pageSearch1 > tbody > tr.gray').attr("onclick");
setTimeout(functionName, 0)
$('#pageSearch').css({'visibility': 'hidden'});
return false;
}
});
});
</script>
「onClick」属性は次のスクリプトです。
function insertPageIDIntoHiddenField(pageID,pageName)
{
$('tr#eventPageID td#loc input#locationID').val(pageID);
$('tr#eventPageID td#loc input#myID').replaceWith('<input id="myID" class="event_form_long" type="text" name="location" value="'+pageName+'" autocomplete="off" />');
$('tr#eventPageID td#loc input#myID').text(pageName);
$('#pageSearch').replaceWith('<div id="pageSearch"></div>');
}