【问题标题】:Form doesn't get submitted when there's click event on submit button当提交按钮上有点击事件时,表单没有被提交
【发布时间】:2019-01-03 09:35:21
【问题描述】:

我有表单,提交按钮上的点击事件侦听器在哪里。单击提交按钮时,按钮上的事件单击事件侦听器会被触发,但不会提交表单。

这是我的代码:

<!-- HTML -->
<form action="/customer/create" method="post">
  <input type="text" name="email">
  <button type="submit" class="has-button-spinner">Create Customer</button>
</form>

<!-- JS -->
const buttons = document.querySelectorAll('.has-button-spinner');

buttons.forEach((button) => {
  button.addEventListener('click', (e) => {
    const target = e.currentTarget;

    target.classList.add('is-loading');
    target.setAttribute('disabled', true);
  });
});

【问题讨论】:

    标签: javascript html forms ecmascript-6 ecmascript-5


    【解决方案1】:

    当您禁用 &lt;button&gt; 时,您会阻止其操作(提交表单)发生。

    不要听按钮的click 事件,而是听表单的submit 事件:

    const forms = document.querySelectorAll('form');
    
    forms.forEach((form) => {
      form.addEventListener('submit', (e) => {
        const target = form.querySelector('button.has-button-spinner');
    
        target.classList.add('is-loading');
        target.setAttribute('disabled', true);
      });
    });
    <form action="/customer/create" method="post">
      <input type="text" name="email">
      <button type="submit" class="has-button-spinner">Create Customer</button>
    </form>

    【讨论】:

    • 这是更好的解决方案,但您应该说明为什么它不适用于click 事件
    • @CodingIntrigue 我添加了一个简短的解释。
    • 该死的你在我输入我的时候回答了:(不过很好的答案!
    【解决方案2】:

    正如@Zenoo 所说,收听onClick-event 会阻止执行实际的按钮操作(submit),因此您可以:

    在处理程序中提交表单:

    HTML:

    <!-- add id to form -->
    <form id="form_create_customer" action="/customer/create" method="post">
      <input type="text" name="email">
      <button type="submit" class="has-button-spinner">Create Customer</button>
    </form>
    

    JS:

    const buttons = document.querySelectorAll('.has-button-spinner');
    
    buttons.forEach((button) => {
        button.addEventListener('click', (e) => {
            const target = e.currentTarget;
    
            target.classList.add('is-loading');
            target.setAttribute('disabled', true);
    
            document.getElementById("form_create_customer").submit();
        });
    });
    

    将处理程序更改为收听onSubmit 而不是onClick

    HTML:

    <!-- add id to form -->
    <form id="form_create_customer" action="/customer/create" method="post">
      <input type="text" name="email">
      <button type="submit" class="has-button-spinner">Create Customer</button>
    </form>
    

    JS:

    const buttons = document.querySelectorAll('.has-button-spinner');
    
    buttons.forEach(button => {
        button.parentNode.addEventListener('submit', e => {
            // we're not using the event data to avoid
            // having to find the button from the form
            // on each submit event
            button.classList.add('is-loading');
            button.setAttribute('disabled', true);
        });
    });
    

    【讨论】:

    • @Zenoo 你这是什么意思?
    • @Zenoo 你的意思是button.parentNode.addEventListener?你有没有忽略.parentNode
    • 看来我做到了,对不起^^
    猜你喜欢
    • 1970-01-01
    • 2010-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 2016-01-11
    • 2019-10-16
    • 1970-01-01
    相关资源
    最近更新 更多