''必需"功能似乎与返回确认无关

时间:2018-03-01 07:11:39

标签: javascript html

当我提交不完整的数据时,为什么我的要求不会触发?这是我的代码:

<form class="form-adminpage" method="post" action="admin.php">
                <input type="text" name="project_title" placeholder="Project Title" required /><br>
                <input type="text" name="city" placeholder="City" required /><br>
                <input type="text" name="province" placeholder="Province" required /><br>
                <input type="text" name="cost" placeholder="Cost" required /><br>
                <input type="Submit" name="button" value="Enter" onclick="return confirm('Are you sure you want to add the following data?');">
            </form>

1 个答案:

答案 0 :(得分:1)

onclick将首先触发。如果您想先显示HTML 5验证,可以在表单的onsubmit事件中添加提醒

<form class="form-adminpage" method="post" action="admin.php" onsubmit="return confirm('Are you sure you want to add the following data?');">
    <input type="text" name="project_title" placeholder="Project Title" required="true" /><br>
    <input type="text" name="city" placeholder="City" required /><br>
    <input type="text" name="province" placeholder="Province" required /><br>
    <input type="text" name="cost" placeholder="Cost" required /><br>
    <input type="Submit" name="button" value="Enter" >
</form>