I'd use more HTML5-like style
$(document).ready(function() {
var required = $('[required]');
// bind change for all you just click, and keyup for all textfields
required.bind('change keyup', function() {
var flag = 0;
// check every el in collection
required.each(function() {
if ($(this).not(':checkbox, :radio').val() || $(this).filter(':checked').val()) flag++;
});
// if number of nonempty (nonchecked) fields == nubmer of required fields minus one excess radio input
if (flag==required.length-1) $('input[type=submit]').prop('disabled',false);
else $('input[type=submit]').prop('disabled', true);
});
});
EDIT As my wife noticed (thank you, I love you, darling) this example is not working with checkboxes and radio buttons correctly, because even unchecked, they always return their positive value.
So I updated my code and demo and I want to explain the difference. First of all, I changed condition, now it looks like if ($(this).not(':checkbox, :radio').val() || $(this).filter(':checked').val())
- in the 1st part we delete all checkboxes and radio buttons from collection, and in the 2nd we check value of all "checked" inputs (that means all checked inputs, deleted in 1st part).
The next untidy step is to use a number, like required.length-1
to reduce number of all unnecessary radio fields manually
I'm still trying to find better solution, but now you can check this new demo
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…