简体   繁体   English

如何确认一个公司,但在两者之间设置一个 function?

[英]How to confirm a firm but set a function in-between?

I got a script that checks every form in my app and if there is one, that gets submitted, it will prevent the submission, toggles a loading screen and then submits the form.我有一个脚本,它检查我的应用程序中的每个表单,如果有一个被提交,它将阻止提交,切换加载屏幕,然后提交表单。

<script>
  document.addEventListener('DOMContentLoaded', () => {

      const forms = document.querySelectorAll('form');
      forms.forEach((form) => {
        form.addEventListener('submit', (e) => {
          e.preventDefault();
          document.getElementById('loading').classList.toggle('hidden');
          form.submit();
        })
      });

  });

</script>

This works perfectly but for some forms (like a deletion button) I want to ask to user if this is really what he wants to do:这非常有效,但对于某些 forms(如删除按钮),我想问用户这是否真的是他想要做的:

<form method="post" action="<?=base_url('clients/' . $meeting->cid);?>" onsubmit="return confirm('Are you sure?');">
  <input type="hidden" name="delmeeting" value="<?=$meeting->id;?>">
  <input type="submit" value="Delete" class="button">
</form>

This is in conflict with the first script.这与第一个脚本冲突。 The alert gets shown correct but when I cancel it, the first script comes into place and submits.警报显示正确,但是当我取消它时,第一个脚本到位并提交。

How can I handle this?我该如何处理?

What is important to me:对我来说重要的是:

  • display the loading div every time for each form每次为每个表单显示加载 div
  • having the possibility to have forms with and without a confirm window有可能有 forms 有和没有确认 window
  • keeping the code small保持代码小
  • having individual texts for the confirm有单独的确认文本

Got it:知道了:

const forms = document.querySelectorAll('form');
forms.forEach((form) => {
  form.addEventListener('submit', (e) => {
    if (form.onsubmit === null) {
      e.preventDefault();
      document.getElementById('loading').classList.toggle('hidden');
      form.submit();
    }
  })
});
``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM