여기
<script type="text/javascript">
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 0;
$('#addScnt').live('click', function() {
$('<p><input type="text" name="train[]" autocomplete="off" class="train" /><span class="suggest_q" id="suggest_q"></span><a href="#" id="remScnt">Remove train</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').live('click', function() {
if(i > 2) {
$(this).parents('p').remove();
i--;
}
return false;
});
});
</script>
는 아약스 자동 완성 코드 여기
<script type="text/javascript">
$(document).ready(function() {
$(".train").each(function(index, element) {
$(".train").live("keyup", function() {
$(".suggest_q").html("");
var train = $(this).val();
train = $.trim(train);
if(train)
{
$.ajax({
url: "train_ajax_query.php",
data: "train="+train,
success: function(msg) {
$(".suggest_q").html(msg);
$(".suggest_q ul li").mouseover(function() {
$(".suggest_q ul li").removeClass("hover");
$(this).addClass("hover");
})
$(".suggest_q ul li").click(function() {
var value = $(this).html();
$(element).val(value);
$(".suggest_q ul li").remove();
});
}
});
}
});
});
});
</script>
에게 있습니다 귀하의 코드 자동 완성, 당신은 글로벌 선택기를 사용합니다. 선택한 요소를 저장 한 다음이 변수를 사용해야합니다.
//Changed the live call for an on call since live is deprecated.
$(document).ready(function() {
$("#p_scents").on("keyup", '.train', function() {
//we save vars with the objects we want to work with.
var selectedTrain = $(this);
var selectedSuggest = $(this).siblings('.suggest_q');
selectedSuggest.html("");
var train = selectedTrain.val();
train = $.trim(train);
if(train) {
$.ajax({
url: "train_ajax_query.php",
data: "train="+train,
success: function(msg) {
//the variables here should still be valid, unless you start more than one ajax
//call simultaneously.
selectedSuggest.html(msg);
selectedSuggest.find("ul li").mouseover(function() {
// This can happen when the variable selectedSuggest is no longer valid
//for example, when another try has been done on another .train
$(this).siblings("li").removeClass("hover");
$(this).addClass("hover");
});
selectedSuggest.find("ul li").click(function() {
var value = $(this).html();
//again, the vars can have changed here, since the click can be a long time
//after the event registration
$(this).parents('.suggest_q').siblings('input').val(value);
$(this).parents('.suggest_q').find("li").remove();
});
}
});
}
});
});
코드는 테스트되지 않았지만 제대로 작동해야합니다. 어떤 오류라도 발견하면 의견을 남겨주세요. 나는 편집 할 것입니다.
jaudette, 정말 고마워요! 코드를 구현했지만 제대로 작동하지 않습니다. 페이지의 모든 .suggest_q div에 여전히 결과가 표시되며 제안 된 값을 클릭하면 입력 필드에 추가되지 않습니다. –
jaudette, 클릭 이벤트를 수정했지만 선택한 값을 클릭하면 모든 입력 필드에 나타납니다. –
@ user1725473 방금 ID를 사용하는 것으로 나타났습니다. ID는 고유해야합니다. 대신 클래스를 추가해야합니다. – jaudette