2016-02-28 115 views
0
<img src='plus.jpg' width='80' height='80' onclick="myFunction2()"> 

回报如何在固定的时间内使用javascript函数?

function myFunction2() { 
     var x = document.createElement("INPUT"); 
     x.setAttribute("name", "image[]"); 
     x.setAttribute("type", "file"); 
     document.getElementById("add").appendChild(x); 
} 

我想用myFunction2()仅有4次。 之后,我想停止这个功能。 你能给我任何建议吗? 在此先感谢。

+0

如何使用循环语句? –

+0

我做到了,但我不知道索引号的确切位置...... @RajaprabhuAravindasamy –

+0

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/for –

回答

1

试试这个:

var limit = 1; 
function myFunction2() { 
    if(limit <= 4){ 
    var x = document.createElement("INPUT"); 
    x.setAttribute("name", "image[]"); 
    x.setAttribute("type", "file"); 
    document.getElementById("add").appendChild(x); 
    limit += 1; 
    }else{ 
    alert("Stop"); 
    } 
} 
+0

非常感谢@MohammaeAbed –

0

你为什么不使用一个计数器来检查时的点击次数达到400,这样

HTML:

<img src='plus.jpg' width='80' height='80' onclick="myFunction2()"> 

INTO JS

var myCounter=0; 
    function myFunction2() { 
if(myCounter <4){myCounter++; 
      var x = document.createElement("INPUT"); 
      x.setAttribute("name", "image[]"); 
      x.setAttribute("type", "file"); 
      document.getElementById("add").appendChild(x); 
} 
    } 
1

使用jQuery的解决方案。

var timesClicked = 0; 
$("#imgId").bind("click", function(event) { 

    // do your operations here 

    timesClicked++; 
    if (timesClicked >= 4) { 
    $(this).unbind(event); 
    } 
}); 
+0

谢谢@Darshan –

相关问题