Back

jQuery code not working


My jQuery code is not working. Every seat is a checkbox. I am trying to disable those seats that are booked by other users.

<script>
  $(document).ready(function(){
    $.ajax({
      type: 'GET',
      url: "{!!URL::to('bookedTickets')!!}",
      success:function(data){
        for(var i=0; i<data.length; i++)
        {
          if($(":checkbox").val() == data[i].SeatNumber)
          {
            $(".checkbox").attr("disabled");
          }
        }
      },
      error:function(data){
          alert("Error");
      }

    });
});
</script>

JSON Data received at ajax

[{"SeatNumber":"1"},{"SeatNumber":"2"},{"SeatNumber":"25"},{"SeatNumber":"26"},{"SeatNumber":"10"},{"SeatNumber":"11"}]
johnathansantos replied 2 months ago

val() return the value of the input

prop('checked') return or if the input is checked

<input value='aaa' checked type='checkbox' id='a' class='checkbox'/>
<input value='bbb' type='checkbox' id='b' class='checkbox'/>
$(':checkbox') // list all checkbox 
$(':checkbox').val() // 'aaa'  - value of the first element 
$(':checkbox').eq(0).val() // 'aaa'

$(':checkbox').eq(0).prop('checked') //true
$(':checkbox').eq(1).prop('checked') //fase
$('input.checkbox:checked').prop('checked') //true
$('.checkbox:checked')[0].checked //true

$('#a')[0].checked //true
$('#a').prop('checked') //true

Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us