【发布时间】:2011-02-16 07:00:12
【问题描述】:
我目前有一个大型民意调查,由一个表单、5 个类别、每个类别中的一堆问题以及每个问题的少数单选按钮答案组成。每个类别都位于不同的字段集中,并且每个类别需要不同的最小投票数才能处理表单。
基本上,在提交表单时,我试图让 jQuery 使用 .each() 循环遍历每个字段集,并确定是否满足每个类别的最低投票数,如果没有,则取消表单提交。到目前为止,我可以很好地处理类别,但不会阻止表单在必要时提交。
$('#pollform').submit(function(event){
var formsubmit = event;
$('fieldset').each(function(formsubmit){
catname = $(this).children('input[name=catname]').val(); // Category name
reqvotes = $(this).children('input[name=catcount]').val(); // Minimum required number of votes for current category in loop
numvotes = $(this).children(':checked').size(); // Number of votes for current category in loop
// Check minimum number of votes against actual number cast. Alert user and prevent form from submitting if necessary
if (reqvotes > numvotes)
{
alert('You need to fill out at least ' + reqvotes + ' items for ' + catname + '.');
formsubmit.preventDefault();
}
});
});
有什么想法吗?
【问题讨论】: