Html +js:第三次点击按钮,它将运行第三个功能

html+js: third time click the button,it will run the third function

本文关键字:运行 功能 三个 第三次 +js 按钮 Html      更新时间:2023-09-26

当我第一次点击按钮时,它会出现一个地址选择,当我第二次,第三次点击同一按钮时,它会出现第二个,第三个地址选择,请帮助我如何使用HTML和Javascript实现这个功能,谢谢!

jQuery中的.toggle()函数在较新的版本中已经过时。我们最好设置一个标志,这样做:

$(function () {
  var inputCount = 0;
  $("input").click(function () {
    var functionsArray = [function () {
      alert(1);
    }, function () {
      alert(2);
    }, function () {
      alert(3);
    }];
    functionsArray[inputCount++]();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="button" value="Click Me!" />

你可以这样做:

$('.addAddress').on('click', function() {
  $('.form').append('<input type="text" name="address" id="address"/>');
});
.form input {
  display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="form">
  <label for="address">Address</label>
</form>
<button class="addAddress">+</button>

每次单击+ button时,它都会在表单中插入一个新字段