简体   繁体   中英

Javascript command for a text field that has entered text

This might be a really stupid question but I cannot figure out the javascript command for the site to decide whether or not the text fields are filled based on whether or not the text fields are actually filled. My code is here.

<html>
<head>
<title>Contact Us</title>
<link rel="stylesheet" type="text/css" href="main.css">
<style type="text/css">
#main-container{
padding-left: 3px;
padding-right: 3px;
}
#left-bar{
text-align: right;
}
#left-bar, #right-bar{
border: 2px solid gray;
}
#top-nav-bar{
border-top: 1px solid gray;
border-bottom: 2px solid gray;
border-left: 2px solid gray;
border-right: 2px solid gray;
}
#navTextn{
float: right;
padding-right: 20px;
padding-top:10px;
}
#navTextc{
float: right;
padding-right: 20px;
padding-top:10px;
}
#navTexth{
float: right;
padding-right: 20px;
padding-top:10px;
}
#Contus{
float:right;
padding-right: 30px;
padding-top:10px;
}
#logo{
height: 20px;
width: 100px;
padding-top: 10px;
padding left: 10px;
}


</style>
<script type="text/javascript">
    function validator(){
        if(document.mUs.bfinderm.text)
            alert("Thank you for your submission");
        else
            alert("No text has been entered");
    }
</script>


<title>Tickit</title>
</head>
<body id="body">
<div id="topline">
</div>
<div id="top-nav-bar">
    <a href="Homework.html"><image id="Logo" src="Tickit.png"></a>
    <a id="Contus" href="ContactUs.html">Us</a>
    <a id="navTextn">News</a>
    <a id="navTextc" href="Calendar.html">Calendar</a>
    <a id="navTexth" href="Homework.html">Home</a>
</div>
<table id="main-container">

    <tr>
        <td id="left-bar" valign="top">
            <a id="plinks" >Profile</a><br>
            <a id="plinks" >Mail</a><br>
            <a id="plinks" href="Homework.html">Work Stream</a><br>
            <a id="plinks" href="calendar.html">Calendar</a><br>
            <a id="plinks" href="Events.html">Events</a><br>
            </p>
        </td>
        <td id="main" valign="top">
        If you find any bugs with the website, please leave a message to us in the box below <br> <br>
            <form name="mUs">
                Message Us:<br> <input type="text" name="bfinderm" />
                <input type="button" value="Send" onClick="validator()" />
            </form>
        </td>
        <td id="right-bar" valign="top">

        </td>
    </tr>
</table>
</body>
</html>

Like in a check box, the validator() code would be the checked

<script type="text/javascript">
    function validator(){
        if(document.stackflowrun.stflr.checked)
            alert("Yes it is checked!");
        else
            alert("Check that shit boul!");
    }
</script>

So what is the equivalent validator() command for the text field option?

function validator(){
        if(document.mUs.bfinderm.text)
            alert("Thank you for your submission");
        else
            alert("No text has been entered");
    }
</script>

(text is not it)

jquery: $("#youridhere").val() returns the input. Add: <script src="http://code.jquery.com/jquery-1.9.1.js"></script> to use jquery. Put that before your <script> .

In JavaScript:

if (document.getElementById("bfindem").value.length > 0)
{
               alert("Thank you for your submission");
        else
}
            alert("No text has been entered");
}

In jQuery:

if ($("#bfindem").val().length > 0)
{
               alert("Thank you for your submission");
        else
}
            alert("No text has been entered");
}

Use the value instead of text like this:

function validator(){
        if(document.mUs.bfinderm.value)
            alert("Thank you for your submission");
        else
            alert("No text has been entered");
    }

Using jQuery, modify your function as shown below

function validator(){
    if($('input[name="bfinderm"]').val().length !== 0)
        alert("Thank you for your submission");
    else
        alert("No text has been entered");
}

jQuery DEMO

Using straight Javascript, modify your function as shown here

function validator(){
    if($('input[name="bfinderm"]').val().length !== 0)
        alert("Thank you for your submission");
    else
        alert("No text has been entered");
}

Javascript DEMO

尝试document.mUs.bfinderm.value - 不需要jquery

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