连接到数据库的Checkbox的子复选框

时间:2015-10-14 16:34:29

标签: javascript php jquery html checkbox

我有一个使用php连接到数据库的复选框,我也有来自HTML的子复选框。我的问题是我有从数据库连接的实验室复选框,如果用户检查/点击它,它将出现所有子复选框,如果不是它只隐藏。

这是我的列表复选框的PHP代码

<?php
	$tsql = "select medTestName from medtest";
  $tstmt = $con->prepare($tsql);
  $tstmt->execute();
  $tstmt->bind_result($mtn);
$tstmt->store_result();
													
while ($tstmt->fetch()){
$d1= '<input type="checkbox" name="test[]"
																  value="'.$mtn.'">'.$mtn.'<br>';
echo $d1;
	}		
													
?>		

这是我的html代码,子复选框列表

<div><input type="checkbox" name="topic" value="1"><span>Complete Blood Count</span></div>
<div><input type="checkbox" name="topic" value="2"><span>Blood Typing</span></div>
<div><input type="checkbox" name="topic" value="3"><span>Urinalysis</span></div>
<div><input type="checkbox" name="topic" value="4"><span>RPR/TPHA</span></div>
<div><input type="checkbox" name="topic" value="5"><span>Hepatitis B screening</span></div>
<div><input type="checkbox" name="topic" value="6"><span>Fasting Blood Sugar</span></div>
<div><input type="checkbox" name="topic" value="7"><span>Creatinine</span></div>
<div><input type="checkbox" name="topic" value="8"><span>Total Cholesterol(Low Cholesterol, High Cholesterol)</span></div>
<div><input type="checkbox" name="topic" value="9"><span>Triglyceride</span></div>
<div><input type="checkbox" name="topic" value="10"><span>VLDL</span></div>
<div><input type="checkbox" name="topic" value="11"><span>Blood Uric Acid</span></div>
<div><input type="checkbox" name="topic" value="12"><span>Anti-HAV Igm Screening</span></div>
<div><input type="checkbox" name="topic" value="13"><span>Anti HBaAg</span></div>
<div><input type="checkbox" name="topic" value="14"><span>Drug & Alcohol Test</span></div>
<div><input type="checkbox" name="topic" value="15"><span>Stool Culture</span></div>

主要复选框的图像

enter image description here

数据库图片

enter image description here

1 个答案:

答案 0 :(得分:1)

不确定我是否理解你想要的东西,但也许这个例子可以帮到你。 (而不是你的sql代码,我使用了一个数组作为例子。)

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var cbxMedTest = $("#formMedTest").find('input:checkbox');
            cbxMedTest.change(function() {
                if (this.id == 73) {
                    $("#subCheckBoxes").toggle();
                }
            });
        });
    </script>
</head>
<body>
<form method="post" id="formMedTest">
    <?php
    $labTests = array(
        71 => "Vital Signs",
        72 => "Neuro-Psychological",
        73 => "Laboratory"
    );
    foreach ($labTests as $key => $labTest) {
        echo sprintf(
            '<input id="%1$d" type="checkbox" name="test[]" value="%2$s">%2$s',
            $key,
            $labTest
        );
    }
    ?>
    <div id="subCheckBoxes" style="display: none;">
        <div><input type="checkbox" name="topic" value="1"><span>Complete Blood Count</span></div>
        <div><input type="checkbox" name="topic" value="2"><span>Blood Typing</span></div>
        <div><input type="checkbox" name="topic" value="3"><span>Urinalysis</span></div>
        <div><input type="checkbox" name="topic" value="4"><span>RPR/TPHA</span></div>
        <div><input type="checkbox" name="topic" value="5"><span>Hepatitis B screening</span></div>
        <div><input type="checkbox" name="topic" value="6"><span>Fasting Blood Sugar</span></div>
        <div><input type="checkbox" name="topic" value="7"><span>Creatinine</span></div>
        <div><input type="checkbox" name="topic" value="8"><span>Total Cholesterol(Low Cholesterol, High Cholesterol)</span></div>
        <div><input type="checkbox" name="topic" value="9"><span>Triglyceride</span></div>
        <div><input type="checkbox" name="topic" value="10"><span>VLDL</span></div>
        <div><input type="checkbox" name="topic" value="11"><span>Blood Uric Acid</span></div>
        <div><input type="checkbox" name="topic" value="12"><span>Anti-HAV Igm Screening</span></div>
        <div><input type="checkbox" name="topic" value="13"><span>Anti HBaAg</span></div>
        <div><input type="checkbox" name="topic" value="14"><span>Drug & Alcohol Test</span></div>
        <div><input type="checkbox" name="topic" value="15"><span>Stool Culture</span></div>
    </div>
    <br>
    <input type="submit" name="submit" value="submit">
</form>
</body>
</html>