【问题标题】:Enable input field after checkbox is checked in list of checkboxes在复选框列表中选中复选框后启用输入字段
【发布时间】:2016-09-06 02:41:03
【问题描述】:

我有一个从数据库获取的数据列表,我想在选中的复选框上启用输入字段。然而,它只适用于第一行。这是 HTML 代码:

<?php
    require_once('inc/config.php');
    include 'verification/verify_form_details.php';
?>
<html>
<head>
  <title>getElementById example</title>
  <script type="text/javascript" src="js/jquery-2.1.3.min.js"></script>
</head>
<body>
    <?php 
        $result2 = getProcessID();
    ?>
    <label>Process: <small style="color:red;">*</small>             
    <?php 
        while ($row = mysql_fetch_array($result2))
        {
            $row[0] = cleanOutputData($row[0]);
    ?>      
    <div>       
        <label>
        <input type="checkbox" class="checkbox" name="process[]" id=<?php echo $row[0] ?>  value=<?php echo $row[0]?> /><?php echo $row[0] ?>
        </label>                                        

        <label>
        <input type="text" class="field" disabled name="number[]" id=<?php echo $row[0] ?> />
        </label>

    </div>
    <?php
        }
        mysql_free_result($result2);
    ?>
    </label>
</body>
</html>

还有javascript函数:

<script>
    $(document).ready(function () {
            $('.checkbox').change(function() {
                $('.field').attr('disabled',!this.checked)
            });
    });
</script>

任何想法我该怎么做?谢谢

【问题讨论】:

  • 因为相同的ID 使用类代替。 ID 应该是唯一的
  • @guradio 我试过上课,现在按下一个复选框后,它启用了所有字段。还编辑了我的问题
  • 使用$(this).parent().next().find('.field').attr('disabled',!this.checked)
  • @guradio 现在所有字段都处于禁用状态,即使选中了复选框
  • 请看下面的答案

标签: javascript php jquery checkbox


【解决方案1】:

$('.checkbox').change(function() {
  $(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>


<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>


<label>
  <input type="checkbox" class="checkbox" name="process[]" id=v alue=/>
</label>

<label>
  <input type="text" class="field" disabled name="number[]" id=/>
</label>
  1. 使用类而不是 ID
  2. 使用此上下文来引用更改的元素

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多