I have a table with 4 columns, and a submit button. Like this:
FOOD YES NO REASON
pizza cb cb tb
fries cb cb tb
curry cb cb tb
noodle cb cb tb
[SUBMIT]
What I need is for the button to be disabled if any No box is checked but its corresponding textbox is empty. I have this working to an extent. If you go through each one in sequence, it's fine. So you say Yes to pizza and the button remains enabled. You change it to No and the button is disabled. You provide a reason and the button is enabled again. You clear the textbox and the button is disabled again. You change back to Yes and the button is enabled. You then do the same for fries and its all fine. Etc.
However, if you choose No for all of them and provide reasons for all of them (button is therefore enabled), but then empty the textbox for pizza, the button doesn't get disabled like it should. But if you clear the noodle one then it does.
What I've been trying to do is cycle through the table every time a checkbox is clicked or on keyup in the Reason column, but I'm quite new to jQuery and don't really know how to do that... Can anyone help?
Thanks.
Code:
$('input[type=checkbox]').click(function(){
checkReasons();
}
$('input[type=text]').keyup(function(){
checkReasons();
});
function checkReasons(){
$('#mytable tr:not(:first)').each(function(){
var cb = $(this).find('td:nth-child(3) input[type=checkbox]');
var tb = $(this).find('td:nth-child(4) input[type=text]').val();
if (cb.is(':checked')){
if (tb.length == 0){
$('#button').attr('disabled','disabled');
} else {
$('#button').removeAttr('disabled','disabled');
}
}
});
}