我有一个带有清除整个表单的重置按钮的表单,但我希望弹出一个确认对话框并确认用户想要重置。
我的确认框可以正常工作,但如何让它在按下确定按钮时重置表单?
下面是我目前的代码:
function reset() {
var r = confirm("Are you sure you want to reset all text?");
if (r == true) {
form.reset();
}
}
<form>
<p>Full Name</p><br><input name="fname" type="text" placeholder="Full Name"><br>
<p>Address</p><input name="address" type="text" placeholder="Address"><br>
<p>Email</p><br><input name="email" type="email" placeholder="Email"><br>
<input type="submit"> <button onclick="reset()" type="button">Reset</button>
</form>
我做错了什么?
最佳答案
试试这个:
HTML:
<input type="reset" onclick="return confirm_reset();">
JS:
function confirm_reset() {
return confirm("Are you sure you want to reset all text?");
}
当onclick
函数返回false
时,它会阻止重置按钮的默认操作。
关于javascript - 在确认框中单击“确定”后如何重置表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22335482/