如何在不验证表单的情况下选择按钮

时间:2019-02-26 19:39:07

标签: html required

当我选择“平均家庭收入”按钮时,如何防止表单检查必填字段。

<form method="post">  The 
<input type="text" name="myIncome[0][Income]" required
  placeholder="#######.##" pattern="^(?=.*[1-9])\d*(?:\.\d{2}$)?" />

<input type="submit" name='cmdSubmit' value="Submit" 
formaction="ProjectCensus.php">

<input type="submit" name='cmdAvgHousehold' value="Average Household Income" 
formaction="Average_household.php">

1 个答案:

答案 0 :(得分:0)

您想更改该按钮的类型。

尝试:

<button type="button" ... ></button>

已根据请求更新: 我建议您使用JQuery ajax()来处理表单的提交(这将是最容易理解的)。

$("#myForm").submit(ev => {
  ev.preventDefault();
  $.ajax({
    type: 'POST',
    url: "Average_household.php",
    // data: {},
    success: resp => {
      alert("Submitted comment");
    },
    error: resp => {
      alert("There was an error submitting this form");
    }
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- src: https://html.com/attributes/button-type/ -->
<form id="myForm" action="/button-type">

  <button type="button" onclick="alert('This button does nothing.')">Click me for no reason!</button>

  <br><br>
  <label for="name">Name</label><br>
  <input name="name"><br><br> <button type="reset">Reset the form!</button><br><br>

  <button type="submit" disabled>Submit (disabled)</button>
</form>

希望这会有所帮助