【发布时间】:2017-08-18 19:04:30
【问题描述】:
这篇文章是关于“必需”输入在“按钮”类型的按钮中不起作用。
我有一个看起来像这样的表单:
<form action="submit.php" method="post">
<label for="username">Start Date</label>
<input id="username" name="inputUsername" type="text" required>
<button type="submit">Submit</button>
<button type="button" onclick="updateUsername()">Update</button>
</form>
当用户点击提交时,表单会运行脚本 submit.php。
当用户点击更新时,会有一个针对不同脚本 update.php 的 AJAX 调用。
这些脚本都运行良好,除了如果您在文本输入为空时单击“更新”按钮,无论如何它都会通过,而不会提醒用户“请填写此字段”。即使它是“必需的”。
我知道发生这种情况的原因是因为它的类型是按钮而不是提交。但是,我不能有两种提交类型,因为如果我有以下内容:
<form action="submit.php" method="post">
...
<button type="submit">Submit</button>
<button type="submit" onclick="updateUsername()">Update</button>
</form>
当我只希望它运行 update.php 时,它将在我的 JavaScript 函数 updateUsername() 中运行 submit.php 脚本和我的其他脚本 update.php。
感谢您的任何建议。
【问题讨论】:
标签: javascript html forms button