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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18038085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:50:40  来源:igfitidea点击:

How to use onsubmit() to show a confirmation if there are multiple submit buttons on the same form?

htmlformsonsubmit

提问by RatDon

<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' />

when the user clicks on second submit button i.e Noi want to display the confirmation dialogue as "Are you sure you want to commit the transaction."

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

回答by RatDon

<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>

Worked fine. just changed onsubmit()to onclick(). as the function of both in this situation is same.

工作得很好。刚换onsubmit()onclick()。因为在这种情况下两者的功能是相同的。

回答by Madison May

You could bind to onclick instead of onsubmit - see below.

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

<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>

Or alternately, using jQuery:

或者,使用 jQuery:

<script> 
$(document).ready(function() {
    $('form input[type=submit]').click(function() {
        return confirm('Rollback deletion of candidate table?');
    });
});
<script>

回答by Tony Ventura

<form onsubmit="submitFunction();">
    <input type='submit' name='delete' value='Undo' />
    <input type='button' onclick="declineFunction()" name='no' value='No' />
</form>

I wouldnt try to create a submit but rather just a button that has a onclick="function()" and then use javascript to set a variable to see how many times they have clicked it and a alert();

我不会尝试创建一个提交,而只是一个具有 onclick="function()" 的按钮,然后使用 javascript 设置一个变量来查看他们点击了多少次和一个 alert();

hope this helps :D

希望这有帮助:D

回答by S. Jensen

Here's an event-listener based solution that avoids inline event handlers (useful if your site has a Content Security Policy that forbids inline JavaScript):

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

HTML:

HTML:

<form method="post">
    <input type="submit" id="deleteButton" name="delete" value="Undo" />
    <input type="submit" id="noButton" name="no" value="No" />
</form>

JS:

JS:

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();
    }
});

回答by totymedli

Just use two of the same form. One for each button:

只需使用两个相同的形式。每个按钮一个:

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

Also, if you would done your research you would find these:

此外,如果您进行研究,您会发现这些: