1

I'm trying to set the input value to 1 when checking the checkbox and empty when unchecking, Can't get it to work, please help.

<td id="check-box"><input type="checkbox" name="checkbox"></td>
<td id="qty-box"><input type="text" name="qtybox"></td>

<script type="text/javascript">
function setValue(a) {
   if (a < 1) {
     a = 1;
   }
}
var qty = $('#qty-box [name="qtybox"]').val();

$("#check-box").click(function() {    
    if ($(this[name = "checkbox"]).attr('checked', true)) {
        setValue(qty);
    }
    else {
        qty = 0;
    }
});
</script>
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
Mosaic
  • 348
  • 3
  • 4
  • 16
  • Just a comment. When a form is posted and a `checkbox` is not checked, it is not included in the `POST`. It is only POSTed when it is checked which might be all you are looking for. If you need to know client-side, you can do so by checking this `$("input[name='checkbox']:checked").length == 1`. – Craig Mar 21 '12 at 15:36

4 Answers4

2

Try the following -

<td id="check-box"><input type="checkbox" name="checkbox"></td>
<td id="qty-box"><input type="text" name="qtybox"></td>

<script type="text/javascript">    
    $(document).ready(function()
    {
        $("input[name='checkbox']").click(function()
        {
            if($(this).is(':checked'))
            { 
                $("input[name='qtybox']").val("1");
            }
            else
            {
                $("input[name='qtybox']").val("");  // Change it to - val("0") - 
                                  //if you want to clear the text box with zero.
            }
        });
    });
</script>

Here is a nice little demo of the working version.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
1

Nice and short with a jsFiddle example:

$('input[name="checkbox"]').change(function(){
    $('input[name="qtybox"]').val($(this).is(':checked')?'1':'');
})​
j08691
  • 204,283
  • 31
  • 260
  • 272
  • One more thing please, How about the reverse, to un-check when the qtybox value is deleted? – Mosaic Mar 21 '12 at 16:11
  • See an updated jsFiddle at http://jsfiddle.net/j08691/NG7Xq/1/. This unchecks the checkbox if the text field is empty and checks it if it's not. – j08691 Mar 21 '12 at 16:20
  • Thanks, but that doesn't check when value is put in to the qtybox. I really appreciate your help, I'm very new to this and programing in general. – Mosaic Mar 21 '12 at 16:33
  • BTW I think you also answered another question for me two weeks ago. – Mosaic Mar 21 '12 at 16:36
  • If i have multiple 's I do something like this? $('input[name="qtybox"]').keyup(function() { $(this).parent().siblings().find('input[name="checkbox"]') .attr("checked", $(this).val().length > 0); – Mosaic Mar 21 '12 at 17:11
  • Hmmm, I'm not sure that would do it. Let me see if I can create a quick example. – j08691 Mar 21 '12 at 17:13
  • Please take a look at my new question, nobody seems to have a clue. http://stackoverflow.com/questions/9811271/calculate-one-td-x-another-td-in-jquery – Mosaic Mar 21 '12 at 19:41
  • May I ask you for some help, http://stackoverflow.com/questions/10971913/move-mysql-fetch-array-pointer/10972137#comment14328144_10972137 – Mosaic Jun 10 '12 at 20:44
0

This will do

$(function(){

    $('input[name="checkbox"]').click(function(){
        $('input[name="qtybox"]').val(0);
        if($(this).is(':checked'))
        {
            $('input[name="qtybox"]').val(1);
        }

    });

});

Working sample : http://jsfiddle.net/PBzuQ/17/

Shyju
  • 214,206
  • 104
  • 411
  • 497
0

Give the id to the input makes thing much more simple.

<td><input type="checkbox" id="check-box" name="checkbox"></td>
<td><input type="text" id="qty-box" name="qtybox"></td>

<script type="text/javascript">
$(function () {
    $("#check-box").click(function () {
        var qty = $('#qty-box');
        if ($(this).prop('checked')) {
            if (qty.val() < 1) qty.val(1);
        } else {
            qty.val(0);
        }
    });
});
</script>
xdazz
  • 158,678
  • 38
  • 247
  • 274