Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Associate input type checkbox to input type text

I have a problem, I need to associate a input type checkbox to a input type text.

The situation is as follows:

Extract data from a database. The PK data are the value of the checkbox. When a checbox select an input type text where you can enter a specific number is activated.

Now the problem is that selecting a checkbox input text all kinds are activated. And what I hope is that by selecting the checkbox input only the input associated with the checkbox enabled.

My HTML code (This code creates a input checkbox and input text for each record in the database, and what I want is to activate a specific checkbox is activated, the input type text):

<form action="send.php" method="POST" id="form-touch">
    <?php foreach ($data as $key): ?>
        <div id="inputsForm">
              <input type="checkbox" class="id-check" name="nuevoid[]" value="<?php echo $key['pr_id'] ?>">
                <input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
        </div>
    <?php endforeach; ?>
                <input type="submit" value="Send">
</form>

My jquery code (code that activates or deactivates the text field by selecting a checkbox):

$('.id-check').change(function(){
    if ($('.id-check').is(':checked') == false){
         $('.myinput').val('').prop('disabled', true);
   } else {
         $('.myinput').val('1').prop('disabled', false);
   }
});

How I can achieve what I want? Greetings from Chile.

like image 608
Cristian Meza Avatar asked May 21 '26 09:05

Cristian Meza


1 Answers

Try

$('.id-check').change(function() {
  if ($(this).is(':checked') == false) {
    $(this).closest('div').find('.myinput').val('').prop('disabled', true);
  } else {
    $(this).closest('div').find('.myinput').val('1').prop('disabled', false);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="send.php" method="POST" id="form-touch">

  <div id="inputsForm">
    <input type="checkbox" class="id-check" name="nuevoid[]" value="">
    <input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
  </div>
  <div id="inputsForm">
    <input type="checkbox" class="id-check" name="nuevoid[]" value="1">
    <input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
  </div>

  <input type="submit" value="Send">
</form>
like image 90
Tamil Selvan C Avatar answered May 23 '26 00:05

Tamil Selvan C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!