我正在尝试使用 id="train" 对任何 div 进行自动完成,但是,它似乎只在第一个输入字段上使用“train”类
javascript代码:
<script type="text/javascript">
$(document).ready(function() {
$(".train").each(function() {
$(".train").keyup(function() {
$("#suggest_q").html("");
var train = $(".train").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();
$("#train").val(value);
$("#suggest_q ul li").remove();
});
}
});
}
});
});
});
</script>
HTML 代码:
<form id="train_create_form" name="train" action="submit_train.php" method="POST">
<input type="text" id="train" class="train" size="20" name="train[]" placeholder="Train 1" />
<div id="suggest_q">
</div>
<input type="text" id="train" size="20" class="train" name="train[]" placeholder="Train 2" />
<div id="suggest_q">
</div>
<input id= "submit_train" type="submit" name="submit_train" value="Submit All trains">
</form>
我找到了这篇旧帖子,但我不确定如何解决我的问题:(
添加更多输入字段代码:
<script type="text/javascript">
$(function() {
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 0;
$('#addScnt').live('click', function() {
$('<p><input type="text" id="train" name="train[]" class="train" placeholder="Train ' + i +'" /><div id="suggest_q"></div><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>