as per a previous question을 기반으로 <select>
드롭 다운에 집중할 수있는 jQuery 코드가 있습니다.jQuery keypress() 핸들러 : 이벤트 흐름을 관리합니다.
HTML
<select id="poetslist">
<option value="shakespeare" data-initial="WS">William Shakespeare</option>
<option value="milton" data-initial="JM">John Milton</option>
<option value="keats" data-initial="JK">John Keats</option>
<option value="wordsworth" data-initial="WW">William Wordsworth</option>
<option value="larkin" data-initial="PL">Phillip Larkin</option>
</select>
자바 스크립트
$("#userlist").keypress(function (e) {
initials += String.fromCharCode(e.which).toUpperCase();
// when the relevant initials are typed, select that item
$(this).find("option").filter(function() {
return $(this).attr("title").toUpperCase().indexOf(initials) === 0;
}).first().attr("selected", true);
$('#book_results').html(loading_img); // Show 'loading' gif
// Then use get_books function to update some HTML on the page with relevant info
get_books($(this).find("option").filter(function() {
return $(this).attr("title").toUpperCase().indexOf(initials) === 0;
}).first().val(), null, null);
if (timer) {
clearTimeout(timer);
timer = null;
}
timer = setTimeout(function() {
timer = null;
initials = "";
}, 2000); // Max 2 second pause allowed between key presses
return false;
});
그것은 매우 유망한하지만 난 그 때 사용자 유형, 예를 들어 찾는거야 : 여기있다 'WW', 'William Shakespeare'가 먼저 선택되고 페이지의 모든 HTML이 업데이트되어 2 초 후에 드롭 다운이 'catch up'되고 'William Wordsworth'에 대한 업데이트가 수행됩니다.
나는 이것이 이벤트의 흐름을 정확하게 처리하지 못하는 것에 대한 내 잘못이라는 것을 확신하지만 누구나 수정 제안을 할 수 있습니까? N.B. 모든 상품에 두 개의 이니셜이 있음을 보장 할 수는 없습니다.
자바 스크립트 초보자를 도와 주셔서 감사합니다! ...