0

i have a loop in my laravel project which contain checkbox and input

@foreach($s as $sh)
<tr>
<td><input type="checkbox" name="service_id[]" class="checkser" value="{{$sh->id}}"></td>
<td class="checkservice">{{$sh->name}}</td>
<td> <input type="text" class="form-control checkser inputservice" placeholder="{{trans('home.quantity')}}" name="ser_quantity[]" ></td>
<td>{{$sh->price}}</td>
</tr>
@endforeach

i need to fill text input with (1) when checkbox checked
and empty this when checkbox unchecked

i tried many method but failed to work this with next input only

2 Answers 2

2

You can do this using jquery and bind a change event handler to your checkbox.

Also, use closest method.See reference here.

$('.checkser').change(function() {
    if($(this).is(":checked")) {
       $(this).closest('tr').find('.inputservice').val(1);
    }
    else
       $(this).closest('tr').find('.inputservice').val('');        
});
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for your help<br> i used this before but it fill all <br> how to check the next one only '$('.checkservice').change(function() { if($(this).is(":checked")) { $(this).next('.inputservice').val(1); } else $$(this).next('.inputservice').val(''); });`
sorry check my edit , it's working well with mono field but it's not working with loop i tried .next but not working
Please take a look at my answer.
2

Try jQuery is() method

if($(".checkser").is(':checked'))
    $(".inputservice").val(1);  // checked
else
    $(".inputservice").val('');  // unchecked

Just use $(selector).is(':checked')

It returns a boolean value.

1 Comment

thanks for your help , i tried the first comment method and worked well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.