views:

104

answers:

1

Hi guys,

Not sure of the best way to explain this... I have an input where you can enter two times, but also a checkbox if you wish to omit a certain day, thus rendering the inputs kinda useless, so I'd like to disable them...

Here's what I have at the moment for the HTML (repeated 7 times):

<td><div>From:</div><input type="text" name="F_MON"></td>
<td><div>To:</div><input type="text" name="T_MON"></td>
<td class="unavailableday"><input type="checkbox" name="MON_UNAVAIL" value="Y"> Unavailable?</td>

And then the Javascript:

$("tr td input[type=checkbox]").bind("click", function() {
    var $this = $(this);
    if($this.is(':checked')) {
     $this.parents('tr').css({'background-color' : 'lightyellow'});
     $this.parents('tr input').siblings('input type=[text]').attr("disabled", true);
    }
});

Unfortunately when you check the box, the appropriate tr goes yellow which is perfect, but the previous inputs don't disable... I'd also like to do an .unmask() and change the .val() when they're disabled, kinda hoping if I can get the parents/siblings right it'd be easy to adapt :o

Thanks!

+1  A: 

Try

$("tr td :checkbox").bind("click", function() {
    var $this = $(this);
    if($this.is(':checked')) {
        var $row = $this.closest('tr');
        $row.css('background-color', 'lightyellow');
        $this.siblings(':text').attr("disabled", "disabled");
    }
});
redsquare
Absolutely perfect, thank you so much! It's working an absolute treat now :) One quick question... is there any alternative to the 'disabled' option? I don't want to let the user change the contents, but I do want the information to be posted... readonly doesn't seem to work, could be the mask() function I'm using? I did try unmasking but no joy... Thanks again!
Nick
You could hide the input and copy the current value into a label which you show in place of the hidden input.
redsquare
That'd be great actually, thank you! Might just do a PHP if statement to see if the box is checked, could be an okay solution too.Thanks again!
Nick