开发者

Jquery: Execute command, only when all fields of specific pattern are checked

I have a survey with 90 statements, grouped in categories. Easy category has apporximate 10 statements. On each statement the user has to put a score from 0 to 3, presented by radiobuttons. When all statements of a category have a score, the mean has to be calculated.

At the moment I have the calculation, but it's executed after every check of a radio-button and not only when all statements have a score. H开发者_如何转开发ow can I only execute it after all statements have a score? (and not after giving a score after every statement)

The code for calculating and showing the mean:

var avg = $('input[id^=' + answer[0] + ']:checked').avg();
$('span[id=avg_' + answer[0] + ']').text(avg);

An example with HTML-code: http://jsfiddle.net/SFPue/


if($('input:checked').length === ($('input').length) / 4) {
   var avg = $('input[id^=' + answer[0] + ']:checked').avg();
   $('span[id=avg_' + answer[0] + ']').text(avg);
}

Checks if the length of the checked inputs is the same as the length of the inputs. If you have more inputs on the page, you can add a class to the radio button and check for that, or check for inputs with the type='radio'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜