Javascript 而循环增量值在函数内部不起作用
我有以下代码:Javascript 而循环增量值在函数内部不起作用,javascript,jquery,Javascript,Jquery,我有以下代码: <?php $i = 5; while($i > 0){ echo " <input type = 'submit' value = '$i' id = '$i'> "; $i--; } ?> <script type = 'text/javascript' src = 'js/jquery.js'></script> <script type = 'text/javascript'> $(document).re
<?php
$i = 5;
while($i > 0){
echo "
<input type = 'submit' value = '$i' id = '$i'>
";
$i--;
}
?>
<script type = 'text/javascript' src = 'js/jquery.js'></script>
<script type = 'text/javascript'>
$(document).ready(function(){
var i = 5;
while(i > 0){
$('#'+i).live('click',function(){
alert(i);
});
i--;
}
});
</script>
这是因为变量i
中的一些
$(document).ready(function(){
var i = 5;
while(i > 0){
(function(idx){
$('#'+idx).live('click',function(){
alert(idx);
});
})(i)
i--;
}
});
这是因为变量i
中的一些
$(document).ready(function(){
var i = 5;
while(i > 0){
(function(idx){
$('#'+idx).live('click',function(){
alert(idx);
});
})(i)
i--;
}
});