如何验证两个输入-jquery
我有几组问题,每组问题包含2个输入字段如何验证两个输入-jquery,jquery,validation,Jquery,Validation,我有几组问题,每组问题包含2个输入字段 <!--Question Set--> <tr class="options_row"> <td> <img src="images/top_bullet.gif" alt="bullet" /> I will only be satisfied with a
<!--Question Set-->
<tr class="options_row">
<td>
<img src="images/top_bullet.gif" alt="bullet" />
I will only be satisfied with a usually high standard of living </td>
<td width="5%">
<input name="score[]" type="text" class="score num" title="1" maxlength="1" />
</td>
</tr>
<tr class="options_row">
<td>
<img src="images/top_bullet.gif" alt="bullet" />
I wish to have considerable influence over other people. </td>
<td width="5%">
<input name="score[]" type="text" class="score num" title="2" maxlength="1" />
</td>
</tr>
我只会满足于通常高标准的生活
我希望对别人有相当大的影响力。
现在我需要验证每一组输入值
比如说
->问题集1->输入\u 1+输入\u 2>=3
->问题集1->Input_1+Input_2您可以为每个问题集添加一个额外的数据属性:
<input name="score[]" type="text" data-set="1" class="score num" title="1" maxlength="1" />
这将返回如下对象:
questionSet = {
1: {
1: "value1",
2: "value2"
},
2: {
1: "value1",
2: "value2"
}
}
现在,您可以迭代此对象:
for(x in questionSet){
if (x[1] + x[2] >= 3) {
// do something...
} else {
// do something else...
}
}
毕竟这是我想到的
(function($) {
$.fn.calVal = function(params) {
params = $.extend({value:3,message:'The combined value should not be greater than or less than 3'});
//Get the current node
var $t = $(this);
//If the node out of focus
$t.blur(function() {
//get the current group
var data_set = $(this).attr('data-set');
//the actual sum
var total = 0;
//loop through the current node to get the group of its own
$t.each(function(){
//get the group of sellected node
var cur_data_set = $(this).attr('data-set');
//if any matching group of the current node
if(cur_data_set==data_set)
{
//total the value of each selected node group
total += parseFloat($(this).val());
}
});
//Validate the group value and return the result
if(total>params.value)
{
alert(params.message);
$(this).val('');
}else if(total<params.value)
{
alert(params.message);
$(this).val('');
}
});
// allow jQuery chaining
return this;
};
})(jQuery);
(函数($){
$.fn.calVal=函数(参数){
params=$.extend({value:3,消息:'组合值不应大于或小于3'});
//获取当前节点
var$t=$(本);
//如果节点没有焦点
$t.blur(函数(){
//获取当前组
var data_set=$(this.attr('data-set');
//实际金额
var合计=0;
//循环当前节点以获取其自己的组
$t.each(函数(){
//获取所选节点的组
var cur_data_set=$(this.attr('data-set');
//如果当前节点的任何匹配组
if(当前数据集==数据集)
{
//合计每个选定节点组的值
total+=parseFloat($(this.val());
}
});
//验证组值并返回结果
如果(总计>参数值)
{
警报(参数消息);
$(this.val(“”);
}否则,如果(我已将此答案标记为有用,但我尚未接受。直到我尝试上述解决方案;)我真的非常感谢您的时间和努力。
(function($) {
$.fn.calVal = function(params) {
params = $.extend({value:3,message:'The combined value should not be greater than or less than 3'});
//Get the current node
var $t = $(this);
//If the node out of focus
$t.blur(function() {
//get the current group
var data_set = $(this).attr('data-set');
//the actual sum
var total = 0;
//loop through the current node to get the group of its own
$t.each(function(){
//get the group of sellected node
var cur_data_set = $(this).attr('data-set');
//if any matching group of the current node
if(cur_data_set==data_set)
{
//total the value of each selected node group
total += parseFloat($(this).val());
}
});
//Validate the group value and return the result
if(total>params.value)
{
alert(params.message);
$(this).val('');
}else if(total<params.value)
{
alert(params.message);
$(this).val('');
}
});
// allow jQuery chaining
return this;
};
})(jQuery);