Html 如果同一表单上有多个提交按钮,如何使用onsubmit()显示确认?

Html 如果同一表单上有多个提交按钮,如何使用onsubmit()显示确认?,html,forms,onsubmit,Html,Forms,Onsubmit,当用户单击第二个提交按钮,即否时,我想显示确认对话框“您确定要提交交易吗?”只需使用两个相同的表单即可。每个按钮一个: <form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')"> <input type='submit' name='delete' value='Undo' /> <input type='submit' name

当用户单击第二个提交按钮,即
时,我想显示确认对话框“您确定要提交交易吗?”

只需使用两个相同的表单即可。每个按钮一个:

<form onsubmit="return confirm('Are you sure you want to rollback deletion of candidate table?')">
<input type='submit' name='delete' value='Undo' />
<input type='submit' name='no' value='No' />

此外,如果你进行研究,你会发现:



很好。
刚刚将
onsubmit()
更改为
onclick()
。因为在这种情况下两者的功能是相同的。

您可以绑定到onclick而不是onsubmit-请参见下文

<form method='post'>
    <input type='submit' name='undo' value='Undo' onclick="return confirm('Are you sure you want to rollback deletion of candidate table?')"/>
    <input type='submit' name='no' value='No' onclick="return confirm('Are you sure you want to commit delete and go back?')"/>
</form>

函数submitForm(){
返回确认(‘回滚删除候选表?’);
}
或者,使用jQuery:

<script> 
function submitForm() {
    return confirm('Rollback deletion of candidate table?');
}
<script>

<form>
    <input type='submit' onclick='submitForm()' name='delete' value='Undo' />
    <input type='submit' onclick='submitForm()' name='no' value='No' />
</form>

$(文档).ready(函数(){
$('form input[type=submit]')。单击(函数(){
返回确认(‘回滚删除候选表?’);
});
});

我不会尝试创建一个submit,而是创建一个带有onclick=“function()”的按钮,然后使用javascript设置一个变量,以查看他们单击它的次数和一个alert()


希望这有帮助:D

这里有一个基于事件监听器的解决方案,可以避免内联事件处理程序(如果您的站点有禁止内联JavaScript的内容安全策略,则该解决方案很有用):

HTML:


这是常见的。在一个表单中有多个提交按钮吗?我想可能是问题按钮的重复,应该有不同的确认消息。如果表单上有验证会中断表单的发布,则此解决方案没有那么有用,即使表单没有发布,onclick也会触发。不过,如果将验证作为onclick的一部分调用,您仍然可以使用它。
<script> 
function submitForm() {
    return confirm('Rollback deletion of candidate table?');
}
<script>

<form>
    <input type='submit' onclick='submitForm()' name='delete' value='Undo' />
    <input type='submit' onclick='submitForm()' name='no' value='No' />
</form>
<script> 
$(document).ready(function() {
    $('form input[type=submit]').click(function() {
        return confirm('Rollback deletion of candidate table?');
    });
});
<script>
<form onsubmit="submitFunction();">
    <input type='submit' name='delete' value='Undo' />
    <input type='button' onclick="declineFunction()" name='no' value='No' />
</form>
<form method="post">
    <input type="submit" id="deleteButton" name="delete" value="Undo" />
    <input type="submit" id="noButton" name="no" value="No" />
</form>
document.getElementById("deleteButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to rollback deletion of candidate table?")) { 
        evt.preventDefault();
    }
});

document.getElementById("noButton").addEventListener("click", function(evt) {
    if (!confirm("Are you sure you want to commit the transaction?")) { 
        evt.preventDefault();
    }
});