我已经找到了答案,却找不到答案!
我有一个简单的表格,
<form action="adminprocess.php" method="POST">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
在处理表单之前,如何调整此项以确认?
我试过onclick,但无法让它正常工作。
有什么想法吗?
更新 - 我现在拥有的。
<script type="text/javascript">
var el = document.getElementById('myCoolForm');
el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
</script>
<form action="adminprocess.php" method="POST" id="myCoolForm">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
答案 0 :(得分:42)
HTML:
<form action="adminprocess.php" method="POST" id="myCoolForm">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
JavaScript的:
var el = document.getElementById('myCoolForm');
el.addEventListener('submit', function(){
return confirm('Are you sure you want to submit this form?');
}, false);
编辑:你总是可以使用这样的内联JS代码:
<form action="adminprocess.php" method="POST" onsubmit="return confirm('Are you sure you want to submit this form?');">
<input type="submit" name="completeYes" value="Complete Transaction" />
</form>
答案 1 :(得分:16)
<input type="submit" onclick="return confirm('Are you sure you want to do that?');">
答案 2 :(得分:2)
正确的事件是onSubmit(),它应该附加到表单上。虽然我认为可以使用onClick,但onSubmit是正确的。
答案 3 :(得分:1)
如果你已经在使用jQuery ..你可以使用事件处理程序在提交之前触发
$(document).ready(function() {
$("#formID").submit(function(){
// handle submission
});
});
答案 4 :(得分:0)
var submit = document.querySelector("input[type=submit]");
/* set onclick on submit input */
submit.setAttribute("onclick", "return test()");
//submit.addEventListener("click", test);
function test() {
if (confirm('Are you sure you want to submit this form?')) {
return true;
} else {
return false;
}
}
&#13;
<form action="admin.php" method="POST">
<input type="submit" value="Submit" />
</form>
&#13;
答案 5 :(得分:0)
就我而言,我没有表单ID,也无法在form
标记中添加内联。我最终得到了以下jQuery代码
var form = $("form").first();
form.on('submit', function() {
return confirm('Are you sure you want to submit this form?');
});
答案 6 :(得分:-2)
如果您有多个提交按钮执行不同的操作,您可以这样做。
<input TYPE=SUBMIT NAME="submitDelete" VALUE="Delete Script" onclick='return window.confirm("Are you sure you want to delete this?");'>