正在验证javascript中的复选框

Validating Checkboxes in javascript

本文关键字:复选框 javascript 验证      更新时间:2023-09-26

大家好,我是Javascript的新手,不知道如何验证我的复选框。我已经查找了很多示例,但仍然不理解。有人能告诉我如何验证它吗?还有我的下拉菜单,请提前帮我验证。

My Java Script and HTML
function validateForm(){
    var fname, lname, sex, address, email, length, songs, a, i, check, error;
    a=0;
    check=false;
    error=false;
    fname=document.getElementById("firstname").value;
    lname=document.getElementById("lastname").value;
    sex=document.getElementsByName("sex");
    address=document.getElementById("address").value;
    email=document.getElementById("email").value;
    length=document.getElementById("len").value;
//  songs=document.getElementByName("f_song");
    if(fname=="" || fname==null){
        alert("please input a first name");
        error=true;
        return false;
    }
    if(lname=="" || lname==null){
        alert("please input your last name");
        error=true;
        return false;
    }
    for(i=0; i<sex.length; i++){
        if(sex.item(i).checked==false){
                a++;
        }
    }
    if(a==sex.length){
            alert("Please select Male or Female");
            error=true;
            return false;
    }
    if(address=="" || address==null){
        alert("Please input your address thanks");
        error=true;
        return false;

    }
    if(email=="" || email==null){
        alert("Please enter a email address");
        error=true;
        return false;
    }
    if(length=="" || length==null){
        alert("Be select how long have you been a fan");
        error=true;
        return false;
    }



    alert("Am working ");
}

MY HTML

<head>
<title>] </title>
<link rel="stylesheet" type="Text/css" href="wit_frm.css"/>
<script type="text/javascript" src="java.js">
    </script>
</head>
<body>
    <div id="container">
        <div id="header">
        </div>
        <div id="topnav">
            <div id="link">
                <a href="wit.html">Home</a>
                <a href="http://www.whitneyhouston.com">Fan Site</a>
                <a href="wit_form.html">Register</a>
            </div>
        </div>

        <div id="contentarea">
            <form method="post" action="" onsubmit= "return validateForm();">
            <fieldset>
            <legend>Personal Information</legend>
            First Name:<input type="text" name="fname" id="firstname"/>
            <br/>
            <br/> Last Name:<input type="text" name="lname" id="lastname"/>
            <br/>
            <br/> Sex:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;  <input type="radio" name="sex" id="m"/>Male  <input type="radio" name="sex" id="f"/>Female
            <br/>
            <br/>Address:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<textarea id="address" name="add" rows="4" cols="30"></textarea>
            </fieldset>
            <fieldset>
            <legend> User Information</legend>
            <br/>Email: &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp; <input type="text" name="e_address" id="email"/>
            <br/>
            <br/>No of years listening  to her music: <select name="n_years" id="len">
                                                <option value="less than a yr"> less than 1yr</option>
                                                <option value="2-5 years">2-5 years</option>
                                                <option value="5+ years">5+ years</option>
                                                </select>
            <br/>                                   
            <br/>Favorite Songs:I will always love you<input type="checkbox" name="f_song" value="I will always love you"/> I look to you<input type="checkbox" name="f_song" value="I look to you"/></p>  
                            <br/> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  I have nothing<input type="checkbox" name="f_song" value="I have nothing"/> One moment in time<input type="checkbox" name="f_song" value="one moment in time"/></p>
                            <br/>&nbsp;  &nbsp;  &nbsp;  &nbsp; &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; I wanna dance with somebody<input type="checkbox" name="f_song" value="I wanna dance with somebody"/> Greatest Love of All<input type="checkbox" name="f_song" value="Greatest love of all"/></p>
            </fieldset>
            <br/><input type="submit" name="sub_button" value="SUBMIT"/> &nbsp;  &nbsp;  &nbsp; <input type="reset" name="res_button" value="CLEAR"/>
            </form>

        </div>
        <div id="footerfix">
        </div>
        <div id="footer">
             <div id="link_1">
                <a href="wit.html">Home</a>
                <a href="">Fan Site</a>
                <a href="">About Her</a>
            </div>
        </div>
    </div>


</body>
</html>

我会这样做:

纸盒示例

var f_song = document.getElementsByName("f_song");
var oneSongSelected = 0;
//Loop through the checkbox elements, and tally the selected
for(var ss = 0; ss < f_song.length; ss++){
  if(f_song[ss].checked){
    oneSongSelected++;
  }
}
//If none are selected, then alert your error, and return false
if(oneSongSelected === 0){
  alert("Please select at least one song");
  return false;
}

您可以使用jquery,但您可以执行以下操作:

在你的html代码中,你可以使用第一个下拉选项"选择"和值"-1":

<select id="dropDown">
    <option value="-1">Select</option>
</select>

复选框:

<input type="checkbox" name="checkbox" id="checkbox"  />

DropDown JavaScript:

var aValue;
aValue = document.getElementById("dropDown").value;
if(aValue == "-1"){
    return false;
}

复选框JavaScript:

var checkbox = document.getElementById('checkbox');
if(!checkbox.checked){
    return false;
}

由于您是JavaScript新手,我建议您学习jQuery,它比JavaScript更简单。如果你有复选框,并且你想验证它们,你可以试试这个:

$('#checkbox').click(function(){
    if (this.checked) {
    }
});

代码说明

$('#checkbox').click(function()=>这行说"当单击#checkboxid的元素时,执行此函数中的操作。"您需要将#checkbox替换为复选框的id(例如:#yourCheck)或其类的id(示例:.yourClass)。

if (this.checked)=>此if语句检查元素是否已检查。当复选框被选中时,您应该在括号内包含您的代码。

EDIT:这是我制作的一个JSFiddle示例。