当用户提交表单时,会触发writeToStorage
并检查用户是否在localStorage中,如果是,则运行警报。在此警报之后,我不希望该操作执行。我试过return false;
,但显然这并没有阻止python脚本运行。如果触发警报,如何停止表单的操作?
function writeToStorage() {
var chooser = localStorage.getItem("chooser");
if (chooser) {
alert("you are already a user);
//return false; }
else {
//the rest of the program } };
# if alert is run the action of the form should not run
<form name="choice_form" id="choice_form" action="/g/choicehandler" method="post" onsubmit="writeToStorage()">
<textarea name="choice" rows="7" cols="50"></textarea><br />
<input type="hidden" name="chooser" id="form_chooser" />
<input type="submit" value="submit your choice">
</form>
答案 0 :(得分:4)
重新添加return false
并更改:
onsubmit="writeToStorage()"
要:
onsubmit="return writeToStorage();"