我正在使用此代码,效果很好。现在我只需要更改输入名称,input1,输入2等等。
<script type="text/javascript">
$(document).ready(function() {
$("#input1 > select.profissao").live('change', function(){
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "drop1.php",
success: function(html) {
$("#input1 > select.estatistica").html(html);
}
});
});
});
</script>
为什么这个版本不起作用?我已经检查了lint中的代码,并且没有检测到任何错误。 基本上如果我复制上面的代码并将input1更改为input2,效果很好,但我的目标是减少冗余。
<script type="text/javascript">
$(document).ready(function() {
for (i=1; i<3; i++) {
$("#input"+i+" > select.profissao").live('change', function(){
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "drop1.php",
success: function(html) {
$("#input"+i+" > select.estatistica").html(html);
}
});
});
}
});
</script>
编辑:输出类似于<option value=2>Artes</option><option value=1>Humanidades</option>
,但这不会添加到html中
循环我的下拉简单停止工作
答案 0 :(得分:3)
你可以尝试
<script type="text/javascript">
$(document).ready(function() {
for (i=1; i<3; i++) {
(function(idx){
$("#input"+idx+" > select.profissao").live('change', function(){
$.ajax({
type: "POST",
data: "data=" + $(this).val(),
url: "drop1.php",
success: function(html) {
$("#input"+idx+" > select.estatistica").html(html);
}
});
});
})(i);
}
});
</script>
您的函数在范围内获得i
的相同引用。
答案 1 :(得分:0)
顶部和底部块有不同的选择类是一个问题吗?
此外,连接方法似乎不太可维护。
也许考虑使用像这样的东西
$('#input1, #input2, #input3').children('select.area').each(function () {
$(this).live(...);
});
编辑:还交换了选择的html内容,即支持得不是很好,导致了很多错误,所以你可能要考虑每次重建选择