如何在警告后取消表单的操作?

1 投票
1 回答
621 浏览
提问于 2025-04-17 06:20

当用户提交表单时,writeToStorage 这个函数会被触发,它会检查用户是否在本地存储中。如果用户在本地存储里,就会弹出一个提示框(alert)。在这个提示框出现后,我不想让后面的操作继续执行。我试过用 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>

1 个回答

4

把你的 return false 重新加上,然后把下面的内容改成:

onsubmit="writeToStorage()"

改成:

onsubmit="return writeToStorage();"

撰写回答