js循环 函数传参
要实现点击按钮中间的数值变化,三行组件是用for循环出来的,下面是我的代码,但是每一个都不能实现功能,大神帮忙改一下······<script>varnums;windo...
要实现点击按钮中间的数值变化,三行组件是用for循环出来的,下面是我的代码,但是每一个都不能实现功能,大神帮忙改一下······
<script>
var nums;
window.onload = function()
{
for(var i=0;i<3;i++){
nums = document.getElementById("num"+i).value;
document.getElementById("c"+i).onclick = changeNum(i);
document.getElementById("b"+i).onclick = changeNumA(i);
}
};
function changeNum(i)
{
nums++;
document.getElementById("num"+i).value = nums;
}
function changeNumA(i)
{
if(nums > 0){
nums--;
document.getElementById("num"+i).value = nums;
}
else{
document.getElementById("num"+i).value = 0;
}
}
</script>
<body>
<%for(int i=0;i<3;i++){ %>
<form action="#" method="post">
<input type="button" id="b<%=i %>" value="-"/>
<input type="text" id="num<%=i %>" readonly="readonly" value="<%=i+1 %>" style="width: 20px"/>
<input type="button" id="c<%=i %>" value="+"/>
</form>
<%} %>
</body> 展开
<script>
var nums;
window.onload = function()
{
for(var i=0;i<3;i++){
nums = document.getElementById("num"+i).value;
document.getElementById("c"+i).onclick = changeNum(i);
document.getElementById("b"+i).onclick = changeNumA(i);
}
};
function changeNum(i)
{
nums++;
document.getElementById("num"+i).value = nums;
}
function changeNumA(i)
{
if(nums > 0){
nums--;
document.getElementById("num"+i).value = nums;
}
else{
document.getElementById("num"+i).value = 0;
}
}
</script>
<body>
<%for(int i=0;i<3;i++){ %>
<form action="#" method="post">
<input type="button" id="b<%=i %>" value="-"/>
<input type="text" id="num<%=i %>" readonly="readonly" value="<%=i+1 %>" style="width: 20px"/>
<input type="button" id="c<%=i %>" value="+"/>
</form>
<%} %>
</body> 展开
2个回答
展开全部
<script>
window.onload = function()
{
var anony = function(i) {
document.getElementById("c" + i).onclick = function(){
changeNum(i);
};
document.getElementById("b" + i).onclick = function(){
changeNumA(i);
};
};
for (var i = 0; i < 3; i++) {
anony(i);
}
};
function changeNum(i)
{
var num = document.getElementById("num" + i);
num.value = parseInt(num.value) + 1;
};
function changeNumA(i)
{
var num = document.getElementById("num" + i);
num.value = parseInt(num.value) - 1;
num.value = num.value < 0 ? 0 : num.value;
};
</script>
<body>
<form action="#" method="post">
<% for (int i = 0; i < 3; i++){ %>
<input type="button" id="b<%=i %>" value="-"/>
<input type="text" id="num<%=i %>" readonly="readonly" value="<%=i+1 %>" style="width: 20px"/>
<input type="button" id="c<%=i %>" value="+"/>
<%} %>
</form>
</body>
展开全部
var anony = function(i) {
document.getElementById("c" + i).onclick = function(){
changeNum(i);
};
document.getElementById("b" + i).onclick = function(){
changeNumA(i);
};
};
for (var i = 0; i < 3; i++) {
anony(i);
}
document.getElementById("c" + i).onclick = function(){
changeNum(i);
};
document.getElementById("b" + i).onclick = function(){
changeNumA(i);
};
};
for (var i = 0; i < 3; i++) {
anony(i);
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询