所以我有一张表格,提交后需要确认。我正在使用Javascript确认提示。但是,如果有人点击取消,我不希望它刷新。我认为放一个空的其他声明会这样做,但看起来我错了。
如果有人在弹出窗口上按下取消,我不知道我需要做什么来确保它不会刷新。
function postConfirm() {
if (confirm('You will not have another chance after submitting to view your post. Please make sure it is exactly how you want it before continuing. ')) {
yourformelement.submit();
} else {
}
}
<div id="uploadForm">
<center>
<form enctype="multipart/form-data" action="functions/post_upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="99999999" />
<div><textarea id="text" cols="70" rows="15" name="entry"></textarea></div>
<p> Attach an image to this memory!</p>
<input name="userfile" type="file"><br>
<input type="submit" value="Submit" class="blueButton" onclick="postConfirm()"/></div>
</form></center>
</div>
答案 0 :(得分:8)
变化:
onclick="postConfirm()"
到
onclick="return postConfirm()"
并在你的函数中,创建else:
else {
return false;
}
<强> jsFiddle example 强>
答案 1 :(得分:3)
尝试添加
return false;
到if语句的else部分。
修改强>
同时修改对函数的调用,以便返回false传递回提交操作(单击按钮时会触发)。
<input type="submit" value="Submit" class="blueButton" onclick="return postConfirm()"/></div>
答案 2 :(得分:0)
您可以使用事件对象的preventDefault()
方法阻止表单提交,并使用表单元素的submit()
方法在确认后手动提交表单。
将您的HTML
更改为:
<input type="submit" value="Submit" class="blueButton" onclick="postConfirm(event)"/></div>
function postConfirm(e) {
e.preventDefault();
if(confirm('You will not have another chance after submitting to view your post. Please make sure it is exactly how you want it before continuing. '))
yourformelement.submit();
}
但是,最好避免使用内联javascript,因为它被认为是一种不好的做法。相反,你可以这样做:
var button = document.getElementsByClassName("blueButton")[0];
button.addEventListener("click",function(e) {
e.preventDefault();
if(confirm('You will not have another chance after submitting to view your post. Please make sure it is exactly how you want it before continuing. '))
yourformelement.submit();
});
答案 3 :(得分:-1)
尝试
<input type="button" value="Submit" class="blueButton" onclick="postConfirm()"/></div>
而不是
<input type="submit" value="Submit" class="blueButton" onclick="postConfirm()"/></div>