【问题标题】:button submitting form without type submit没有类型提交的按钮提交表单
【发布时间】:2021-07-27 02:03:00
【问题描述】:

我有这个:

<form action="/newPost" method="POST" style="max-width:450px; margin:50px auto;">
        <a href="/media">See Current Posts</a>
          <input name="postTitle" type="text" class="f-input" placeholder="Subject">
          <input name="socialLink" type="text" class="f-input" placeholder="Social Link">
          <textarea name="postDescription" class="f-input" placeholder="Type a message"></textarea>
          <input type="text" name="url" id="url" required readonly>
          <button id="upload_widget" class="button">Upload Photo</button>
          <br>
          <br>
          <button type="submit" value="Send">Send</button>
      </form>

然而,当我点击这个&lt;button id="upload_widget" class="button"&gt;Upload Photo&lt;/button&gt;按钮时,它实际上提交了表单,但是,我没有定义type=submit!!!它不应该提交表单,除非它作为类型提交。我错了吗?我该如何解决这个问题?

【问题讨论】:

    标签: javascript html ejs


    【解决方案1】:

    the specification:

    缺失值默认和无效值默认为提交按钮状态。

    &lt;button&gt; 元素的默认typesubmit

    如果您希望它只触发 JavaScript,那么您必须明确将其设置为 type="button"

    【讨论】:

    • 哇,不知道这个。太棒了,谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-03-21
    • 2010-10-11
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多