将输入类型复选框与输入类型文本关联


Associate input type checkbox to input type text

我有一个问题,我需要将输入类型复选框与输入类型文本关联起来。

情况如下:

从数据库中提取数据。PK数据是复选框的值。当复选框选择一个可以输入特定数字的输入类型文本时,该文本将被激活。

现在的问题是,选择复选框输入文本的所有类型都会被激活。我希望通过选择复选框输入,只启用与复选框关联的输入。

我的HTML代码(此代码为数据库中的每条记录创建一个输入复选框和输入文本,我想要的是激活一个特定的复选框,即输入类型文本):

<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>

我的jquery代码(通过选择复选框来激活或停用文本字段的代码):

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

我怎样才能实现我想要的?来自智利的问候。

尝试

$('.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>

您可以使用.next()jQuery选择器。我已经在下面修改了您的jQuery更改处理程序。它对我有效,所以试试吧。它也应该对你有效。

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

祝你好运,编码快乐!:-)