Javascript 如何使用jquery插件验证按名称动态创建的复选框属性
我正在验证调查问题,它包含单选类型问题和复选框类型问题…单选类型问题工作正常,复选框不工作…在名称字段中,我将问题\u id作为单选按钮的名称传递,将问题\u id+选项\u id作为复选框的名称传递 如果我只为复选框输入问题id,它正在验证,但我需要检查多个选项 //无线电广播Javascript 如何使用jquery插件验证按名称动态创建的复选框属性,javascript,php,jquery,jquery-plugins,Javascript,Php,Jquery,Jquery Plugins,我正在验证调查问题,它包含单选类型问题和复选框类型问题…单选类型问题工作正常,复选框不工作…在名称字段中,我将问题\u id作为单选按钮的名称传递,将问题\u id+选项\u id作为复选框的名称传递 如果我只为复选框输入问题id,它正在验证,但我需要检查多个选项 //无线电广播 <input type="radio" class=" radio" name="31" value="101" style="width: auto;display: inline-block;height
<input type="radio" class=" radio" name="31" value="101" style="width: auto;display: inline-block;height: auto;"> Good
<input type="radio" class=" radio" name="31" value="102" style="width: auto;display: inline-block;height: auto;"> Average
很好
平均值
//用于复选框
<input type="checkbox" class="required check" id="20" name="20_85" style="width: auto;display: inline-block;height: auto;" value="85"> Not good
<input type="checkbox" class="required check" id="20" name="20_84" style="width: auto;display: inline-block;height: auto;" value="84"> Good
不好
好
我需要验证这个复选框
//单击jquery验证
<script type="text/javascript">
$(function() {
$(window).off("resize");
$(window).on("resize", function() {
var newHeight = $("body").outerHeight() + 20;
if($("html").outerHeight() > newHeight) {
newHeight = $("html").outerHeight();
}
parent.postMessage(newHeight, '*');
}).trigger("resize");
var validationParameters = {
rules: {
mobileNumber1: {
required: true,
digits: true,
rangelength: [10,10]
}
},
errorPlacement: function(error, element) {
if($(element).prevAll("label:first").length > 0) {
$(element).prevAll("label:first").after(error);
}
else {
$(element).parent().prevAll("label:first").after(error);
}
$(window).trigger("resize");
},
success: function(label) {
$(window).trigger("resize");
}
};
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1;
if(isAndroid) {
validationParameters.onkeyup = function(element, event) { };
validationParameters.focusInvalid = function(element, event) { };
validationParameters.onfocusout = function(element, event) { };
}
$("#customerMakeReservation2Mobile").validate(validationParameters);
$("#continueButton").click(function() {
if($("#customerMakeReservation2Mobile").valid()) {
var phone = $("#mobileNumber1").val();
$("#phone1").val(phone.substr(0, 3));
$("#phone2").val(phone.substr(3, 3));
$("#phone3").val(phone.substr(6, 4));
$("#customerMakeReservation2Mobile").submit();
}
});
});
</script>
$(函数(){
$(窗口)。关闭(“调整大小”);
$(窗口).on(“调整大小”,函数(){
var newHeight=$(“body”).outerHeight()+20;
if($((“html”).outerHeight()>newHeight){
newHeight=$(“html”).outerHeight();
}
parent.postMessage(newHeight,“*”);
}).触发(“调整大小”);
var validationParameters={
规则:{
手机号码1:{
要求:正确,
数字:对,
rangelength:[10,10]
}
},
errorPlacement:函数(错误,元素){
if($(元素).prevAll(“标签:第一”).length>0){
$(元素).prevAll(“标签:第一”).after(错误);
}
否则{
$(元素).parent().prevAll(“标签:第一”).after(错误);
}
$(窗口)。触发器(“调整大小”);
},
成功:功能(标签){
$(窗口)。触发器(“调整大小”);
}
};
var ua=navigator.userAgent.toLowerCase();
var isAndroid=ua.indexOf(“安卓”)>-1;
if(isAndroid){
validationParameters.onkeyup=函数(元素,事件){};
validationParameters.focusInvalid=函数(元素,事件){};
validationParameters.onfocusout=函数(元素,事件){};
}
$(“#customerMakeReservation2Mobile”).validate(validationParameters);
$(“#继续按钮”)。单击(函数(){
if($(“35; customerMakeReservation2Mobile”).valid(){
var phone=$(“#mobileNumber1”).val();
$(“#phone1”).val(phone.substr(0,3));
$(“#phone2”).val(phone.substr(3,3));
$(“#phone3”).val(phone.substr(6,4));
$(“#customerMakeReservation2Mobile”).submit();
}
});
});
为什么要验证复选框?用户调查反馈…用户必须选择两个复选框回答您实际上可以有重复的id,但这毫无意义,因为您只能在JavaScript代码中检索其中一个id。名称(如果是数组)和类可以是相同的,但如果它们是自然集合,这是有意义的。然后,您可以使用jQuery轻松地对它们进行循环。好吧……我将更改id的名称,如……,那么我如何验证它?您没有发布validate()
函数的代码。验证逻辑是什么??你想用这些选择器做什么?