亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

OnSubmit 調用調用 3 個函數并檢查 return_type 的函數,但未調用 1 個函數

OnSubmit 調用調用 3 個函數并檢查 return_type 的函數,但未調用 1 個函數

PHP
臨摹微笑 2022-07-16 17:03:07
從用戶獲取值的表單,OnSubmit 調用函數 check_three_func()<form method="post" action="register_action.php" id="register_form" name="register_form" onsubmit="return check_three_func()">  <br>  <div class="form-group">    <label for="uname">Name:</label>    <input type="text" class="form-control" id="uname" placeholder="Enter Name " name="uname">  </div>  <div class="form-group">    <label for="uemail">Email id: </label>    <input type="email" class="form-control" id="uemail" placeholder="Enter Email ID" name="uemail">    <!-- onkeyup="javascript:validate(this.value)" -->    <span id="alert" style="display:none"></span>  </div>  <div class="form-group">    <label for="upassword">Enter Password:</label>    <input type="password" class="form-control" id="upassword" placeholder="Set password" name="upassword">  </div>  <div class="form-group">    <label for="ucpassword">Confirm Password:</label>    <input type="password" class="form-control" id="ucpassword" placeholder="Enter password again" name="ucpassword" >  </div>  <!-- captcha div -->  <div class="form-group">    <img src="captcha.php" class="rounded" alt="Captcha"><br>    <label for="captcha">Enter Captcha: </label>    <input type="text" class="form-control" id="captcha"  name="captcha" maxlength="6">     <!-- onkeyup="javascript:captcha_func(this.value)" -->     <span id="captcha-result" style="display:none"></span>  </div>  <button type="submit" class="btn btn-success" id="submit-button" >Submit</button></form>check_three_func() 在代碼中調用下面提到的 3 個函數并返回 return_type,因此如果為 false 則無法提交表單function check_three_func(){    var check_email_func=check_email();    var click_to_func=click_to();    var check_func=check();    if( check_email_func==true && click_to_func==true && check_func==true){      return true;      console.log("all true");    }    else{      return false;      console.log(" false");    }}
查看完整描述

2 回答

?
飲歌長嘯

TA貢獻1951條經驗 獲得超3個贊

將異步參數值更新為 false。如果 async 參數值為 false,則 send() 方法在收到響應之前不會返回。


xhttp.open("GET","emailvalidate.php?uemail="+email,false);

試試下面的代碼它會工作。


function check_email(){

  var email = document.getElementById("uemail").value;

  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

    xhttp=new XMLHttpRequest();

}

 else {// code for IE6, IE5

    xhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }

  result = true; // create a variable result and by default make it true.

  xhttp.onreadystatechange=function(){

    if (xhttp.readyState == 4 && xhttp.status == 200) {

      document.getElementById("alert").style.display="inline";

      if(xhttp.responseText=="EMP"){

        document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-info'>fill out emai id</span>";

        console.log("Email id empty return false");

        result = false;

      }

      else if(xhttp.responseText=="OK"){

      document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-success' >welcome new user</span>";

      //document.getElementById("submit-button").disabled = false;

      console.log("New email id return true");

      }

      else if(xhttp.responseText=="NO"){

      document.getElementById("alert").innerHTML="<br><span class='badge badge-pill badge-danger'>Email Already Exist</span>";

      //document.getElementById("submit-button").disabled = true;

      console.log("Email id already exsist return false");

      result = false;

      }

      else{

        document.getElementById("alert").innerHTML=xhttp.responseText;

        //document.getElementById("submit-button").disabled = true;

        console.log("Error fetching email id");

        result = false;

      }

    }

  };

  xhttp.open("GET","emailvalidate.php?uemail="+email,false);

  xhttp.send();


  return result;  //at last return the result.

}


查看完整回答
反對 回復 2022-07-16
?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

這check_email是一個異步函數。這就是為什么你沒有得到返回結果。你必須調用下面的函數。


function在關鍵字之前添加 async并在check_email function.


async function check_three_func() {

    var check_email_func = await check_email();

    var click_to_func = click_to();

    var check_func = check();

    if (check_email_func && click_to_func && check_func) {

        return true;

        console.log("all true");

    }

    else {

        return false;

        console.log(" false");

    }

}

將您的 ajax 代碼包裝在一個 Promise 中,并根據響應解決或拒絕,然后返回該 Promise。

function check_email() {


    return new Promise((resolve, reject) => {

        var email = document.getElementById("uemail").value;

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari

            xhttp = new XMLHttpRequest();

        }

        else {// code for IE6, IE5

            xhttp = new ActiveXObject("Microsoft.XMLHTTP");

        }

        xhttp.onreadystatechange = function () {

            if (xhttp.readyState == 4 && xhttp.status == 200) {

                document.getElementById("alert").style.display = "inline";

                if (xhttp.responseText == "EMP") {

                    document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-info'>fill out emai id</span>";

                    console.log("Email id empty return false");

                    reject(false);

                }

                else if (xhttp.responseText == "OK") {

                    document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-success' >welcome new user</span>";

                    //document.getElementById("submit-button").disabled = false;

                    console.log("New email id return true");

                    resolve(true);

                }

                else if (xhttp.responseText == "NO") {

                    document.getElementById("alert").innerHTML = "<br><span class='badge badge-pill badge-danger'>Email Already Exist</span>";

                    //document.getElementById("submit-button").disabled = true;

                    console.log("Email id already exsist return false");

                    reject(false);

                }

                else {

                    document.getElementById("alert").innerHTML = xhttp.responseText;

                    //document.getElementById("submit-button").disabled = true;

                    console.log("Error fetching email id");

                    reject(false);

                }

            }

        };

        xhttp.open("GET", "emailvalidate.php?uemail=" + email, true);

        xhttp.send();

    });


}


查看完整回答
反對 回復 2022-07-16
  • 2 回答
  • 0 關注
  • 112 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號