简体   繁体   English

HTML / Javascript-checkValidity()不显示需要的位置

[英]HTML/Javascript - checkValidity() doesn't show where is required

New into HTML and JS. HTML和JS的新功能。 I have been trying to figure out why I am not getting any notification while I am not entering any Name in a field. 我一直想弄清楚为什么在字段中未输入任何名称时却没有收到任何通知。 a quick video of how it is looking currently: VIDEO of entering name 有关其当前外观的快速视频: 输入名称的视频

My code is looking 我的代码正在寻找

HTML: HTML:

  <!-- ================ form start ================= --> 
  <form class="form-search form-search-position">
    <div class="container">
      <div class="row">
        <div class="col-sm gutters-19">
          <div class="form-group">
            <label for="FormControlSelect1">Fullständiga namn</label>
            <input class="form-control" required id="namn" type="text" placeholder="Skriv in för- och efternamn..">
          </div>
        </div>
      </div>

      <div class="row">
        <div class="col-sm gutters-19">
          <div class="form-group">
            <button class="button button-form" id="toggle" onsubmit="return false;" onclick="myFunction()" type="button">Beställning</button>
          </div>
        </div>
      </div>
    </div>
  </form>
  <!-- ================ form end ================= --> 

JS: JS:

<script>

  function myFunction() {

    var inpObj = document.getElementById("namn");
    if (!inpObj.checkValidity()) { /** Kollar ifall namnet input är skriven **/
      document.getElementById("toggle").innerHTML = inpObj.validationMessage;
    } else {
      var data = {'namn': namn.value}

      var json = JSON.stringify(data) /** Konventera till JSON **/

      let req = new XMLHttpRequest();

      req.onreadystatechange = function() {
        if (this.readyState == 4) {

          if (this.status == 200) { /** Kollar ifall request response är 200 = OK **/
            console.log("Response is looking good! [" + this.status + "]");

          } else { /** Om det är inte OK. Skicka meddelande till användaren.  **/
            console.log("Response is bad! [" + this.status + "] - Check if the server is on and connected!");
            document.getElementById("toggle").innerHTML = "Gick ej att beställa!";
            document.getElementById("toggle").disabled = true;
          }
        }
      };

      req.open("post", "https://localhost:44363/api/values"); 
      req.setRequestHeader("content-type", "application/json");
      req.send(json);
      document.getElementById("toggle").innerHTML = "Tack för beställning";
      document.getElementById("toggle").disabled = true;

      setTimeout(function(){
       window.location.reload(1);
     }, 5000);

    } 
  } 

</script>

 function myFunction() { var inpObj = document.getElementById("namn"); if (!inpObj.checkValidity()) { /** Kollar ifall namnet input är skriven **/ document.getElementById("toggle").innerHTML = inpObj.validationMessage; } else { var data = { 'namn': namn.value } var json = JSON.stringify(data) /** Konventera till JSON **/ let req = new XMLHttpRequest(); req.onreadystatechange = function() { if (this.readyState == 4) { if (this.status == 200) { /** Kollar ifall request response är 200 = OK **/ console.log("Response is looking good! [" + this.status + "]"); } else { /** Om det är inte OK. Skicka meddelande till användaren. **/ console.log("Response is bad! [" + this.status + "] - Check if the server is on and connected!"); document.getElementById("toggle").innerHTML = "Gick ej att beställa!"; document.getElementById("toggle").disabled = true; } } }; req.open("post", "https://localhost:44363/api/values"); req.setRequestHeader("content-type", "application/json"); req.send(json); document.getElementById("toggle").innerHTML = "Tack för beställning"; document.getElementById("toggle").disabled = true; setTimeout(function() { window.location.reload(1); }, 5000); } } 
 <!-- ================ form start ================= --> <form class="form-search form-search-position"> <div class="container"> <div class="row"> <div class="col-sm gutters-19"> <div class="form-group"> <label for="FormControlSelect1">Fullständiga namn</label> <input class="form-control" required id="namn" type="text" placeholder="Skriv in för- och efternamn.."> </div> </div> </div> <div class="row"> <div class="col-sm gutters-19"> <div class="form-group"> <button class="button button-form" id="toggle" onsubmit="return false;" onclick="myFunction()" type="button">Beställning</button> </div> </div> </div> </div> </form> <!-- ================ form end ================= --> 

Basically the issue is that whenever I do not enter any name, it is supposed to either it shows a red "background" on the border or just a small popout to tell the user that it is required to enter a Name here. 基本上,问题在于,只要我不输入任何名称,都应该在边框上显示一个红色的“背景”,或者只是一个小的弹出窗口来告诉用户需要在此处输入名称。

How can I do that? 我怎样才能做到这一点?

When you are checking the validity of the name input object in the following if statement: 在以下if语句中检查名称输入对象的有效性时:

  var inpObj = document.getElementById("namn");
  if (!inpObj.checkValidity()) { /** Kollar ifall namnet input är skriven **/
    document.getElementById("toggle").innerHTML = inpObj.validationMessage;
  } else {
    var data = {
      'namn': namn.value
    }
  }

You set submit button text to the validation message. 您将提交按钮文本设置为验证消息。 What you wanna do is take your input object (inpObj) and change the background color. 您想要做的就是获取输入对象(inpObj)并更改背景颜色。 This can be done as follows: 可以按照以下步骤进行:

  var inpObj = document.getElementById("namn");
  if (!inpObj.checkValidity()) { /** Kollar ifall namnet input är skriven **/
    inpObj.style.backgroundColor = "red";
  } else {
    var data = {
      'namn': namn.value;
    }
    inpObj.style.backgroundColor = "none";
 }

For the display of an error message you want to insert a element below the input element that has a standard display set to none as below: 为了显示错误消息,您想要在输入元素下方插入一个元素,该元素的标准显示设置为none,如下所示:

    <input class="form-control" required id="namn" type="text" placeholder="Skriv in för- och efternamn..">
    <div id="error-message" style="display:none">Please fill in your name</div>

You can than toggle the display as needed by doing the following in your function: 然后,可以通过在函数中执行以下操作来根据需要切换显示:

var errorMessage = document.getElementById("error-message");
errorMessage.style.display = "block";

You can find a snippet below: 您可以在下面找到一个代码段:

 function myFunction() { var inpObj = document.getElementById("namn"); if (!inpObj.checkValidity()) { /** Kollar ifall namnet input är skriven **/ inpObj.style.backgroundColor = "red"; var errorMessage = document.getElementById("error-message"); errorMessage.style.display = "block"; } else { inpObj.style.backgroundColor = "none"; var errorMessage = document.getElementById("error-message"); errorMessage.style.display = "none"; var data = { 'namn': namn.value } var json = JSON.stringify(data) /** Konventera till JSON **/ let req = new XMLHttpRequest(); req.onreadystatechange = function() { if (this.readyState == 4) { if (this.status == 200) { /** Kollar ifall request response är 200 = OK **/ console.log("Response is looking good! [" + this.status + "]"); } else { /** Om det är inte OK. Skicka meddelande till användaren. **/ console.log("Response is bad! [" + this.status + "] - Check if the server is on and connected!"); document.getElementById("toggle").innerHTML = "Gick ej att beställa!"; document.getElementById("toggle").disabled = true; } } }; req.open("post", "https://localhost:44363/api/values"); req.setRequestHeader("content-type", "application/json"); req.send(json); document.getElementById("toggle").innerHTML = "Tack för beställning"; document.getElementById("toggle").disabled = true; setTimeout(function() { window.location.reload(1); }, 5000); } } 
 <!-- ================ form start ================= --> <form class="form-search form-search-position"> <div class="container"> <div class="row"> <div class="col-sm gutters-19"> <div class="form-group"> <label for="FormControlSelect1">Fullständiga namn</label> <input class="form-control" required id="namn" type="text" placeholder="Skriv in för- och efternamn.."> <div id="error-message" style="display:none">Please fill in your name</div> </div> </div> </div> <div class="row"> <div class="col-sm gutters-19"> <div class="form-group"> <button class="button button-form" id="toggle" onsubmit="return false;" onclick="myFunction()" type="button">Beställning</button> </div> </div> </div> </div> </form> <!-- ================ form end ================= --> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM