验证javascript中的复选框
大家好,我是Javascript新手,不知道如何验证我的复选框。我查阅了很多示例,但仍然不理解。请有人告诉我如何验证它,以及我的下拉菜单inst validating。请提前提供任何帮助,我们将不胜感激验证javascript中的复选框,javascript,jquery,validation,Javascript,Jquery,Validation,大家好,我是Javascript新手,不知道如何验证我的复选框。我查阅了很多示例,但仍然不理解。请有人告诉我如何验证它,以及我的下拉菜单inst validating。请提前提供任何帮助,我们将不胜感激 My Java Script and HTML function validateForm(){ var fname, lname, sex, address, email, length, songs, a, i, check, error; a=0; check
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: <input type="radio" name="sex" id="m"/>Male <input type="radio" name="sex" id="f"/>Female
<br/>
<br/>Address: <textarea id="address" name="add" rows="4" cols="30"></textarea>
</fieldset>
<fieldset>
<legend> User Information</legend>
<br/>Email: <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/> 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/> 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"/> <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>
我的Java脚本和HTML
函数validateForm(){
变量fname、lname、性别、地址、电子邮件、长度、歌曲、a、i、支票、错误;
a=0;
检查=错误;
错误=错误;
fname=document.getElementById(“firstname”).value;
lname=document.getElementById(“lastname”).value;
sex=document.getElementsByName(“sex”);
地址=document.getElementById(“地址”).value;
email=document.getElementById(“email”).value;
长度=document.getElementById(“len”).value;
//songs=document.getElementByName(“f_song”);
如果(fname==“”| | fname==null){
警报(“请输入名字”);
错误=真;
返回false;
}
如果(lname==“”| | lname==null){
提醒(“请输入您的姓氏”);
错误=真;
返回false;
}
对于(i=0;i,由于您是JavaScript新手,我建议您学习jQuery,它比JavaScript更容易、更简单。如果您有复选框并且希望验证它们,您可以尝试以下方法:
$('#checkbox').click(function(){
if (this.checked) {
}
});
代码解释
$(“#checkbox”)。单击(function()
=>此行显示“当单击#checkbox
的id
元素时,执行此函数中的操作。”您需要将#checkbox
替换为复选框的id(例如:#yourCheck
)或其类(例如:.yourCheck>)
if(this.checked)
=>此if
语句检查元素是否被选中。当复选框被选中时,您应该在括号内包含代码
编辑:这是我做的一个示例。您可以使用jquery,但您可以这样做:
在html代码中,您可以使用第一个下拉选项“选择”和值“-1”:
复选框JavaScript:
var aValue;
aValue = document.getElementById("dropDown").value;
if(aValue == "-1"){
return false;
}
var checkbox = document.getElementById('checkbox');
if(!checkbox.checked){
return false;
}
我会这样做:
var f_song=document.getElementsByName(“f_song”);
var oneSongSelected=0;
//循环检查复选框元素,并对所选元素进行计数
for(var ss=0;ss
Oh-wow请查看整个代码。此外,您可能希望删除该XML声明。
var aValue;
aValue = document.getElementById("dropDown").value;
if(aValue == "-1"){
return false;
}
var checkbox = document.getElementById('checkbox');
if(!checkbox.checked){
return false;
}
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;
}