[英]How to validate radio buttons?
我创建了以下表单: http : //jsfiddle.net/baumdexterous/GYNSu/1/
我实现了一个验证脚本,该脚本适用于许多表单元素,但由于某种原因,它不适用于单选按钮,因此当前用户可以在不选择所需单选按钮之一的情况下提交表单。
如何使用此现有脚本启用对单选按钮的验证? 非常感谢。
<ul>
<li class="required">
<label>What is the U.S. Capital? Please select your answer below:</label>
<div class="questionsone">
<input name="questionone" type="radio" value="a">a) New York<br>
<input name="questionone" type="radio" value="b">b) Washington DC<br>
<input name="questionone" type="radio" value="c">c) Seattle<br>
<input name="questionone" type="radio" value="d">d) Portland<br>
</div>
</li>
</ul>
更新 :我有这个JavaScript部分,可以与现有验证一起使用。 试图弄清楚如何添加一种检测单选按钮的方法:
<script>
$(function() {
var root = $("#wizard").scrollable();
// some variables that we need
var api = root.scrollable(),
drawer = $("#drawer");
// validation logic is done inside the onBeforeSeek callback
api.onBeforeSeek(function(event, i) {
// we are going 1 step backwards so no need for validation
if (api.getIndex() < i) {
// 1. get current page
var page = root.find(".page").eq(api.getIndex()),
// 2. .. and all required fields inside the page
inputs = page.find(".required :input").removeClass("error"),
// 3. .. which are empty
empty = inputs.filter(function() {
return $(this).val().replace(/\s*/g, '') == '';
});
// if there are empty fields, then
if (empty.length) {
// slide down the drawer
drawer.slideDown(function() {
// colored flash effect
drawer.css("backgroundColor", "#229");
setTimeout(function() {
drawer.css("backgroundColor", "#fff");
}, 1000);
});
// add a CSS class name "error" for empty & required fields
empty.addClass("error");
// cancel seeking of the scrollable by returning false
return false;
// everything is good
} else {
// hide the drawer
drawer.slideUp();
}
}
// update status bar
$("#status li").removeClass("active").eq(i).addClass("active");
});
// if tab is pressed on the next button seek to next page
root.find("button.next").keydown(function(e) {
if (e.keyCode == 9) {
// seeks to next tab by executing our validation routine
api.next();
e.preventDefault();
}
});
});
</script>
使单选按钮名称全部相同。 那把他们聚集在一起。 然后,您要做的就是使第一个被checked="true"
<input name="question" type="radio" value="a" checked="true">a) New York<br>
<input name="question" type="radio" value="b">b) Washington DC<br>
<input name="question" type="radio" value="c">c) Seattle<br>
<input name="question" type="radio" value="d">d) Portland<br>
这使他们发送至少一件事。 否则,您必须检查输入的选定值。
同样,所有文本输入都可以只具有required
属性,并且可以确保其中存在某些内容。 HTML5将其作为验证。
http://www.w3schools.com/tags/att_input_required.asp
编辑这是我刚刚使用jQuery制作的小提琴。 这是您想要的真实验证。 http://jsfiddle.net/slyfox13/H9Dw2/
您的收音机应该都具有相同的名称。 在关于的代码中,您实际上有4个不同的输入,每个输入都是它们自己的表单数据参数,而不是4个影响一个表单数据参数的无线电输入。
输入名称不必全部相同,即,然后检查questionone的值是否不为null以进行验证
为所有单选按钮设置相同的名称,因为单选按钮可供选择。
<input name="questionone" type="radio" value="a">a) New York<br>
<input name="questionone" type="radio" value="b">b) Washington DC<br>
<input name="questionone" type="radio" value="c">c) Seattle<br>
<input name="questionone" type="radio" value="d">d) Portland<br>
使用jQuery进行验证:
if($("input[type=radio,name=questionone]:checked").length >0)
{
/* do something */
}
首先,您需要将单选按钮名称更改为相同的内容。 前
<ul>
<li class="required">
<label>What is the U.S. Capital? Please select your answer below:</label>
<div class="questionsone">
<input name="question" type="radio" value="a">a) New York<br>
<input name="question" type="radio" value="b">b) Washington DC<br>
<input name="question" type="radio" value="c">c) Seattle<br>
<input name="question" type="radio" value="d">d) Portland<br>
</div>
</li>
</ul>
这样您将只能选择一个。 接下来,您需要创建一个document.ready()函数。 将此代码添加到您的document.ready()
$( document ).ready(function() {
function RadioTest(){
inputs = document.getElementsByName('question');
for (index = 0; index < inputs.length; ++index) {
if(inputs[index].checked!=true){
alert("radio not selected");
return false;
}
}
}
});
您必须为所有无线电输入标签指定相同的名称。 像这样
<div class="questionsone">
<input name="qstn1" type="radio" value="a">a) New York<br>
<input name="qstn1" type="radio" value="b">b) Washington DC<br>
<input name="qstn1" type="radio" value="c">c) Seattle<br>
<input name="qstn1" type="radio" value="d">d) Portland<br>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.