Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当复选框已选中时,如何显示输入框?_Javascript_Jquery - Fatal编程技术网

Javascript 当复选框已选中时,如何显示输入框?

Javascript 当复选框已选中时,如何显示输入框?,javascript,jquery,Javascript,Jquery,我有一个php for循环,它返回12个带有输入框的复选框 <div class="row"> <div class="col-md-4"> <?php $getChannel = mysqli_query($conn, "SELECT ch_id, ch_name, ch_for FROM channel WHERE lg_id = '$lg_id' ");

我有一个php for循环,它返回12个带有输入框的复选框

<div class="row">
    <div class="col-md-4">
    <?php                                    
    $getChannel = mysqli_query($conn, "SELECT ch_id, ch_name, ch_for FROM channel WHERE lg_id = '$lg_id' ");

    $ch_for     = array();
    $ch_name    = array();

    while ( $fetchChannel =  mysqli_fetch_array($getChannel) ) {
        $ch_id      = (int) $fetchChannel['ch_id'];
        $ch_for[]   = htmlspecialchars($fetchChannel['ch_for']);
        $ch_name[]  = htmlspecialchars($fetchChannel['ch_name']);
    }

    for ($x=1; $x<=12; $x++) {
        if(  in_array('ch'.$x, $ch_name)) {
            $sel = 'checked = "checked" ';
        } else {
            $sel = '';
        }
        ?>

        <div class="checkbox form-inline">
            <label><input <?php echo $sel; ?> type="checkbox" name="ch_name[]" value="ch<?php echo $x; ?>">CH<?php echo $x; ?></label>
            <input type="text" name="ch_for[]" value="" placeholder="Channel details" class="form-control ch_for">
        </div>                                    
        <?php
    }
    ?>                                    
</div>

您需要的是以下功能:

// no-conflict safe document ready (shorthand version)
jQuery(function($) {
    // hide all of the inputs initially
    $('.ch_for').hide();

    // your original function to show / hide on click
    $('.checkbox input:checkbox').on('click', function() {
        $(this).closest('.checkbox').find('.ch_for').toggle('slow');
    }); 

    // put this in a function for reusability
    function showInputs() {
        // use .each to loop over every checked input
        $('.checkbox input:checkbox:checked').each(function() {        
            // inside .each, $(this) refers to the checkbox element
             $(this).closest('.checkbox').find('.ch_for').show();
        });
    }

    // call the function on initial load
    showInputs();
});

让我试试这个,它在控制台logo中的参数列表后显示
未捕获的语法错误:缺失)是的,对不起,我错过了
上的一个结束(
。每个
。我已经改正了!
// no-conflict safe document ready (shorthand version)
jQuery(function($) {
    // hide all of the inputs initially
    $('.ch_for').hide();

    // your original function to show / hide on click
    $('.checkbox input:checkbox').on('click', function() {
        $(this).closest('.checkbox').find('.ch_for').toggle('slow');
    }); 

    // put this in a function for reusability
    function showInputs() {
        // use .each to loop over every checked input
        $('.checkbox input:checkbox:checked').each(function() {        
            // inside .each, $(this) refers to the checkbox element
             $(this).closest('.checkbox').find('.ch_for').show();
        });
    }

    // call the function on initial load
    showInputs();
});