正在验证javascript中的复选框
Validating Checkboxes in javascript
大家好,我是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: <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>
我会这样做:
纸盒示例
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()
=>这行说"当单击#checkbox
中id
的元素时,执行此函数中的操作。"您需要将#checkbox
替换为复选框的id(例如:#yourCheck
)或其类的id(示例:.yourClass
)。
if (this.checked)
=>此if
语句检查元素是否已检查。当复选框被选中时,您应该在括号内包含您的代码。
EDIT:这是我制作的一个JSFiddle示例。
相关文章:
- 为复选框javascript指定两个值
- Javascript复选框函数:;缺少:在属性id之后"
- JavaScript-切换“;全部检查”;复选框true/false
- 如何从javascript/jquery中的复选框中获取布尔值
- 如何使用javascript函数在gridview中按行启用/禁用复选框
- 使用javascript在页面加载时取消选中所有复选框
- 更改类以选中复选框JavaScript
- 选中所有复选框javascript
- 方法通过复选框javascript获取触发器
- 选中所有复选框 JavaScript
- 复选框 Javascript 函数在 Bootstrap modal 中不起作用
- 使用复选框 JavaScript 禁用按钮
- 隐藏输入未正确选择的自定义复选框.Javascript需要调整
- 不带POSTING值的求和复选框-Javascript
- 自定义警报与复选框- Javascript
- 按钮,根据字符串开始选择复选框- Javascript
- 简单的复选框javascript不起作用
- 禁用和启用复选框Javascript
- 如何计算表单中的所有复选框(Javascript)
- 数据类型检查提示复选框javascript