提问者:小点点

如何在javascript中停止报警后重新加载页面[重复]


我想停止重新加载页面时,我没有选择类别和点击提交按钮。有人能帮我吗?

null

function check() {
  var select = document.getElementById("category").value;
  if (select == -1) {
    return alert("You didn't choose category !!!");
  }
}
<form action="#" method="POST" id="form" name="form">
  <select name="category" id="category">
        <option value="-1">Select category</option>
        <option value="1">News</option>
        <option value="2">Sport</option>
        <option value="3">World</option>
        <option value="4">Music</option>
    </select>
  <input type="submit" value="submit" id="submit" name="submit" onclick="check();" />
</form>

null


共1个答案

匿名用户

您需要从函数返回false以防止表单被提交。

<input type="submit" value="submit" id="submit" name="submit" onclick="return check();" />

function check() {
    var select = document.getElementById("category").value;
    if (select == -1) {
        alert("You didn't choose category !!!");

        return false;
    }
}