基于输入变量的Javascript重复按钮单击

newusertomysqli12

我试图使按钮单击(发送)的次数乘以用户在输入表单中填写的数量(多少)。我的问题是点击没有发生。发生了什么问题,没有错误日志。日志说的是单击次数乘以输入变量的时间。。但是,单击按钮只会发生一次。输入是单击后提交的表单。

简而言之,我的问题是:我需要JavaScript(或其他解决方案)来提交ID为“ send”乘以用户填写ID为“ howmany”的数字的按钮。因此,当用户输入10时,该按钮应仅提交10次,然后停止,直到用户再次按下该按钮。

<tr><td class="footer">Hire for (hours): </td><td class="footer"><input type="number" value="1"  onClick="this.select()" name="findtime" size="20"></td></tr>
<tr><td class="footer">How many times: </td><td class="footer"><input type="number" value="1"  onClick="this.select()" id="howmany" name="howmany" size="20"></td></tr>
<tr><td class="footer"></td><td class="footer"><input class="example_c" type="submit" id="send" name="send" onclick="myFunction()" value="Hire private detective"></td></tr>

<script language="javascript">
 function myFunction() {
  const timesToClick = document.getElementById("howmany").value;
  for (let i = 0; i < timesToClick; ++i) {
    document.getElementById("send").click();
    console.log('clicked');
  }  
}
</script>
李健

您可以使用setTimeOut进行迭代

请运行代码段,单击“开始”以查看效果

function realclick(){
console.log('clicked');
document.getElementById("send").click();
}


function myFunction() {

if (document.getElementById("howmanyleft").value =="") {
document.getElementById("howmanyleft").value=document.getElementById("howmany").value;
}

if (document.getElementById("howmanyleft").value > 0) {
setTimeout(function(){ realclick(); }, 200);
document.getElementById("howmanyleft").value=(document.getElementById("howmanyleft").value *1) -1;
 }
else
{
document.getElementById("howmanyleft").value ="";
}
 
}
  
<table>
<tr><td class="footer">Hire for (hours): </td><td class="footer">
<input type="number" value="1"  onClick="this.select()" name="findtime" size="20"></td></tr>
<tr><td class="footer">How many times: </td><td class="footer">
<input type="number" value="5"  onClick="this.select()" id="howmany" name="howmany" size="20"></td></tr>
<tr><td>
<input  value=""   id="howmanyleft" id="howmanyleft" size="20" readonly type=hidden></td></tr>


<tr><td class="footer"></td>
<td class="footer">
<input class="example_c" type="submit" id="send" name="send" onclick="myFunction()" value="Start"></td></tr>

</table>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章