简体   繁体   English

如何将 javascript 调整为动态生成的复选框

[英]How to adjust javascript to dynamically generated checkboxes

I have a bootstrap modal which is populated with checkboxes which corresponds to the number of data I retrieve from database.我有一个 bootstrap 模式,其中填充了与我从数据库中检索到的数据数量相对应的复选框。 However I have a hard time on the javascript part of it.但是,我在它的 javascript 部分上遇到了困难。

Here is my code snippet for modal:这是我的模态代码片段:

        <!-- Bootstrap Modal - To Add New Record -->
    <!-- Modal -->
<div class="modal fade" id="add_new_record_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header" style="color:blue;">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel" style="color:blue; text-align: center;">Add Submitted Requirements</h4>
            </div>
            <div class="modal-body">
                <?php 
    include_once 'dbconnect.php';
    $query = "SELECT * FROM requirements_tbl";
    if (!$result = mysql_query($query)) {
        exit(mysql_error());
    }

    // if query results contains rows then featch those rows 
    if(mysql_num_rows($result) > 0)
    {

        while($row = mysql_fetch_assoc($result))
        {

             echo'<div class="checkbox ">
            <label> 
            <input type="checkbox" id="chkbox" name="" value="" onclick="EnableDisableTextBox(this)">
            '.$row['requirement_name'].'
            </label>
            <div class="form-group">
            <label for="exampleInputEmaill"> Date </label>
            <input type="date" id="datefield" class="form-control" name="date" placeholder="YYYY-MM-DD" disabled="disabled">
            </div>
            </div>';
        }
    }
    else
    {
        echo "No Records Retrieve";
    }

    ?>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-primary" onclick="addRecord()">Add Record</button>
            </div>
        </div>
    </div>
</div>


<!--- End of add modal -->

And Here is my javascript:这是我的javascript:

  <script type="text/javascript">
 function EnableDisableTextBox(chkbox) {
    var datefield = document.getElementById("datefield");
    datefield.disabled = chkbox.checked ? false : true;
    if (!datefield.disabled) {
        datefield.focus();
    }
}
</script>

when I click the the first or the last checkboxes the first datefield is the one enabling not the datefield with the corresponding checkbox当我单击第一个或最后一个复选框时,第一个日期字段是启用不是带有相应复选框的日期字段的那个

This is supposed to be a comment but i have a low reputation here.这应该是一个评论,但我在这里的声誉很低。

Please do not use mysql.请不要使用mysql。 Consider switching to either PDO or MYSQLI.考虑切换到 PDO 或 MYSQLI。 Also, before displaying values from your query, consider binding them, hence the reason to switch over to either MYSQLI or PDO此外,在显示查询中的值之前,请考虑绑定它们,因此有理由切换到 MYSQLI 或 PDO

Back to your question.回到你的问题。 What happens when you click on a checkbox?单击复选框时会发生什么? Does the function "EnableDisableTextBox" get called?函数“EnableDisableTextBox”是否被调用? What does your console say?你的控制台说什么? any error??有什么错误吗??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM