Method to prohibit page refresh after JS form submission

The desired effect: When changing the password, judge the password, if it does not match, give a prompt, and the page will not be refreshed; the problem encountered: give a prompt, and the page will be refreshed.
Method 1: By using return false; the effect cannot be achieved, and it is used in the click event return Confirm(this)to achieve the desired effect.
return false is only valid in the current function and will not affect the execution of other external functions.

<button class="primary" onclick=" return Confirm(this)">确认</button>
function Confirm() {
    
    
  let originalUserName = $("#originalUserName").val();
  let originalPw = $("#originalPw").val();
  let newPw = $("#newPw").val();
  let newPwAgain = $("#newPwAgain").val();
  if (!(originalUserName && originalPw && newPw && newPwAgain)) {
    
    
     $.showMessage({
    
    
        message: "必填项为空!",
        type: $.MessageType.Error});
     return false;
   }
    if (newPw != newPwAgain) {
    
    
       $.showMessage({
    
    
           message: "两次输入的密码不一致!",
           type: 'Info'});
       return false;
     }
}

Method 2: By looking at the code, it is found that <button class="primary" onclick=" Confirm()">确认</button>this line of code appears in the form form. By modifying <button class="primary" onclick=" Confirm(event)">确认</button>this Confirmmethod and adding the blocking default event e.preventDefault(), the effect can also be achieved.

Guess you like

Origin blog.csdn.net/weixin_44202904/article/details/122298318
Recommended