javascript - 为什么 onclick ="ShowLocalDate()"只工作一次,它遇到的第一个

标签 javascript html

下面的代码中有三种形式。第一种形式可以很好地传递日期,但其他两种形式不能传递日期。其他形式只要是第一个调用的形式就可以正常工作。所以问题是为什么我只能在页面上以一种形式使用 onclick 函数。调用第一个 onclick 后,所有其他 onclick 都将被忽略。

    <!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Active Profile</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

<script>
var buttons = document.querySelectorAll('form');

for (var i = 0; i < buttons.length; i++) {
buttons[i].addEventListener('submit', ShowLocalDate);
}

function ShowLocalDate() {
event.preventDefault();

var dNow = new Date();

var localdate = '[' + (dNow.getMonth() + 1) + ']<' + dNow.getDate() + '>!' +
dNow.getFullYear() + '!-' + dNow.getHours() + '-:' +
dNow.getMinutes() + ':';

event.target.querySelector(".createdAtInput").value = localdate;

event.target.submit();
}

</script>

</head>

<body>
<!-- form below works -->
<form action="history_report.php" method="post">
<input type="text" class="createdAtInput" name="php_client_date" />
<button type="submit">X-fer Date</button>
</form>

<!--  does not work -->
<form action="history_report.php" method="post">
<input type="text" class="createdAtInput" name="php_client_date" />
<button type="submit">X-fer Date</button>
</form>

<!-- does not work -->
<form action="modify_refund_script.php" method="post">
<input type="text" class="createdAtInput" name="php_client_date" />
<button type="submit">X-fer Date</button>
</form>

</body>
</html>

最佳答案

id 属性在同一文档中必须是唯一的,使用常见类替换一次重复项,例如:

<form action="history_report.php" method="post">
    <input type="text" class="createdAtInput" name="php_client_date" />
    <button type="submit">X-fer Date</button>
</form>

最好避免使用内联事件onClick并尝试使用addEventListener()将事件附加到JS代码中> 喜欢:

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Active Profile</title>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>

  <script>
    document.addEventListener("DOMContentLoaded", function(event) {
      var forms = document.querySelectorAll('form');

      for (var i = 0; i < forms.length; i++) {
        forms[i].addEventListener('submit', ShowLocalDate);
      }
    });

    function ShowLocalDate() {
      event.preventDefault();

      var dNow = new Date();

      var localdate = '[' + (dNow.getMonth() + 1) + ']<' + dNow.getDate() + '>!' +
        dNow.getFullYear() + '!-' + dNow.getHours() + '-:' +
        dNow.getMinutes() + ':';

      event.target.querySelector(".createdAtInput").value = localdate;

      event.target.submit();
    }
  </script>

</head>

<body>
  <!-- form below works -->
  <form action="history_report.php" method="post">
    <input type="text" class="createdAtInput" name="php_client_date" />
    <button type="submit">X-fer Date</button>
  </form>

  <!--  does not work -->
  <form action="history_report.php" method="post">
    <input type="text" class="createdAtInput" name="php_client_date" />
    <button type="submit">X-fer Date</button>
  </form>

  <!-- does not work -->
  <form action="modify_refund_script.php" method="post">
    <input type="text" class="createdAtInput" name="php_client_date" />
    <button type="submit">X-fer Date</button>
  </form>

</body>

</html>

关于javascript - 为什么 onclick ="ShowLocalDate()"只工作一次,它遇到的第一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52447130/

相关文章:

javascript - 在 Vue.js 中将文件上传到 ftp 服务器

javascript - 如何在html中嵌入谷歌趋势图?

php - 无法通过 POST 方法将 <SCRIPT> 标记存储到数据库中

html - 是否有类似黑莓的条件注释来验证网络应用程序中的特定元标记?

html - 使用 flexbox 防止下一行占据上一行中最高元素的高度?

javascript 在 Canvas 上绘制图像

javascript - jQuery .ajax() 回调范围困惑

javascript - 计算样式不返回元素的计算宽度

javascript - 选择保存图像的目录

javascript - 刷新内容 jQuery