如何重定向到同一页面

How do I redirect to same page

我应该如何更改我的 javascript 代码以防止它 link 在单击 window 确认 () 上的取消后转到另一个页面?

或者我应该使用jQuery来解决这个问题?

请帮忙。谢谢

function checktext() {
  var x = document.forms["myTable"]["id"].value;
  if (isNaN(x)) {
    alert("Please check ID!");
    return false;
  }
}

function myFunction() {
  var check = confirm("Are You Confirm To Submit?");
  if (check == true) { 
      console.log("yes");
  }
}
<form name="table_field" id="insert_form" method="post" onsubmit="return checktext()" action="apply.inc.php">
  <hr>
  <h1 class="text-center">Overtime Table</h1>
  <hr>
  <table class="table table-bordered" id="myTable">
    <tr>
      <th>ID</th>
      <th>Full Name</th>
    </tr>
    <tr>
      <td><input class="form-control" name="id[]" type="text" id="id" required></td>
      <td><input class="form-control" name="name[]" type="text" id="name" required></td>
    </tr>
  </table>

  <b>Send To:</b>
  <select class="form-control" name="options">
    <option value="-----" id="-----" hidden>-----</option>
    <option value="Table_A">Table A</option>
    <option value="Table_B">Table B</option>
  </select>
  <input type="submit" name="save" id="save" value="Submit" onclick="myFunction()">
</form>

您可以在您的if条件中通过以下代码刷新页面。

window.location.reload()

window.location.href = window.location.href;

window.location.replace(window.location.href);

提交表单时,将调用 submit 事件。该事件包含存储在 SubmitEvent 对象中的一些信息和操作。当您通过 onsubmit 属性、onsubmit 属性 或通过 addEventListener.

侦听事件时,该对象可用

我强烈推荐使用addEventListener,因为属性版本只是一项老技术,它似乎不想消亡,而且有奇怪的副作用addEventListener 没有。

当你不希望一个表单在提交时提交,调用event对象上的preventDefault()方法。这将确保您的表单不会在您需要时发送并且不会重新加载页面。

var form = document.getElementById('insert_form');

function onSubmit(event) {
  var x = event.target.elements["id"].value;
  if (isNaN(x)) {
    alert("Please check ID!");
    event.preventDefault();
    return false;
  }
  
  var check = confirm("Are You Confirm To Submit?");
  if (check == true) { 
    console.log("yes");
  } else {
    event.preventDefault();
  }
}

form.addEventListener('submit', onSubmit);
<form name="table_field" id="insert_form" method="post" action="apply.inc.php">
  <hr>
  <h1 class="text-center">Overtime Table</h1>
  <hr>
  <table class="table table-bordered" id="myTable">
    <tr>
      <th>ID</th>
      <th>Full Name</th>
    </tr>
    <tr>
      <td><input class="form-control" name="id[]" type="text" id="id" required></td>
      <td><input class="form-control" name="name[]" type="text" id="name" required></td>
    </tr>
  </table>

  <b>Send To:</b>
  <select class="form-control" name="options">
    <option value="-----" id="-----" hidden>-----</option>
    <option value="Table_A">Table A</option>
    <option value="Table_B">Table B</option>
  </select>
  <input type="submit" name="save" id="save" value="Submit">
</form>