为什么在我的JS弹出窗口中,我的html表单不起作用?

时间:2019-06-01 14:41:27

标签: javascript html

我弹出一个窗口,说“您确定要删除类别吗?”如果回答“是”,请执行一个表单,但是此表单不起作用? html代码:

var modal = document.getElementById("popup");
var btns = Array.prototype.slice.call(document.querySelectorAll(".remove_openpopup"));
var span = document.getElementsByClassName("close_button")[0];
btns.forEach(function(btn) {
  btn.onclick = function() {
    modal.style.display = "block";
  }
});
span.onclick = function() {
  modal.style.display = "none";
}
span2.onclick = function() {
  modal.style.display = "none";
}
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
<body>
  <a href="#" class="remove_openpopup">remove</a>
  <a href="#" class="remove_openpopup">remove2</a>
  <a href="#" class="remove_openpopup">remove3</a>

  <div id="popup" class="popup">
    <div class="popup_content">
      <span class="close">&times;</span>
      <p>Are you sure you wan't delete this categorie ?</p>
      <button class="button_popup close_button">No</button>
      <button class="button_popup close_button" onclick='document.getElementByClassName("remove").submit()'>Yes</button>
    </div>
  </div>
  <form class="remove" method="post"><input type="hidden" name="remove" /></form>
</body>

测试脚本:

if (isset($_POST['remove'])) {
  echo "a";
}

当我单击“是”时,没有任何反应。 有人有主意吗?

1 个答案:

答案 0 :(得分:1)

您正在使用getElementByClassName,但它不存在。最接近的是getElementsByClassName,它返回具有请求的类名的元素数组,但是在这种情况下,我认为您应该使用getElementById并将id属性设置为您的表单:

<body>
    <a href="#" class="remove_openpopup">remove</a>
    <a href="#" class="remove_openpopup">remove2</a>
    <a href="#" class="remove_openpopup">remove3</a>

    <div id="popup" class="popup">
      <div class="popup_content">
        <span class="close">&times;</span>
        <p>Are you sure you wan't delete this categorie ?</p>
        <button class="button_popup close_button">No</button>
        <button class="button_popup close_button" onclick='document.getElementById("remove").submit()'>Yes</button>
      </div>
    </div>
    <form id="remove" method="post"><input type="hidden" name="remove" /></form>
</body>