简体   繁体   中英

Why can't I display alert if text box has a value?

I'm trying to get a function to run an if statement only if something has no value.

<script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
    function checkNSC() {
        var confirmNSC = $("#SelectNsc");
        if (confirmNSC.length == 0) {
            alert("Please ensure all required fields are filled out")
        }
        else {
            alert("NSC has value")
        }

    }

</script>

HTML:

<input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</div>  

I'm trying to say if the value is 0 then to display an alert saying (Please ensure all fields are filled out". Otherwise say "NSC has a value"

Thanks,

Should be,

var confirmNSC = $("#SelectNsc").val();

Because $("#SelectNsc") return a jquery object, so that $("#SelectNsc").length will always return a value greater than 0.

<script src = "https://jqueryvalidation.org/files/lib/jquery-1.11.1.js" > < /script> 
<script type = "text/javascript" >
  $(document).ready(function() {
    $("#myButton").click(function() {
      var confirmNSC = $("#SelectNsc").val().trim();
      if (confirmNSC.length == 0) {
        alert("Please ensure all required fields are filled out")
      } else {
        alert("NSC has value")
      }
    });

  })
</script>

Fiddle

Try this, It's working fine

 <script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script> <script type="text/javascript"> function checkNSC() { var confirmNSC = $("#SelectNsc"); if (confirmNSC.val() =='') { alert("Please ensure all required fields are filled out") } else { alert("NSC has value") } } </script> <input type="text" id="SelectNsc" /> <button onclick="checkNSC();" id="myButton">My Button</button> </div> 

use this code :

<body>
 <script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
    function checkNSC() {
        var confirmNSC = document.getElementById("SelectNsc").value;
        if (confirmNSC.length == 0) {
            alert("Please ensure all required fields are filled out")
        }
        else {
            alert("NSC has value")
        }

    }

</script>
    <input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</body>

OR , use this code :

<body>
 <script src="https://jqueryvalidation.org/files/lib/jquery-1.11.1.js"></script>
<script type="text/javascript">
    function checkNSC() {
        var confirmNSC = $("#SelectNsc").val();
        if (confirmNSC.length == 0) {
            alert("Please ensure all required fields are filled out")
        }
        else {
            alert("NSC has value")
        }

    }

</script>
    <input type="text" id="SelectNsc" />
<button onclick="checkNSC();" id="myButton">My Button</button>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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