Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.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 如何实施“;显示子复选框”;在php中?_Javascript_Php_Jquery_Checkbox - Fatal编程技术网

Javascript 如何实施“;显示子复选框”;在php中?

Javascript 如何实施“;显示子复选框”;在php中?,javascript,php,jquery,checkbox,Javascript,Php,Jquery,Checkbox,我正在使用面向对象的php或prep语句绑定数据库中的所有医疗测试列表,我的问题是如何在php中实现show subcheckbox?如果单击实验室的复选框,是否显示所有子复选框?我认为javascript不起作用 这是用于医疗测试复选框的php代码 <?php $tsql = "select medTestName from medtest"; $tstmt = $con->prepare($tsql); $tstmt->execute();

我正在使用面向对象的php或prep语句绑定数据库中的所有医疗测试列表,我的问题是如何在php中实现show subcheckbox?如果单击实验室的复选框,是否显示所有子复选框?我认为javascript不起作用

这是用于医疗测试复选框的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;
    }       

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

下面是子复选框的代码

  <?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;
    }       

    ?>
<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>
全血计数
血型
尿液分析
RPR/TPHA
乙型肝炎筛查
空腹血糖
肌酐
总胆固醇(低胆固醇、高胆固醇)
甘油三酯
VLDL
血尿酸
抗HAV-Igm筛查
抗HBaAg
药物和酒精测试
粪便培养
这是我的体检结果

这是我的医疗测试数据库


抱歉发了这么长的帖子,我真的不知道该怎么办:(

这就是你的想法吗

我创建了两个PHP数组:一个包含所有测试,另一个包含所有主题。首先,我们迭代测试数组并打印每个项目。在每次迭代中,我们循环主题并打印每个项目(确保每个元素都有一个唯一的ID)

生成HTML后,我们使用jQuery隐藏包含每个测试主题的字段集。然后,我们将单击处理程序附加到每个测试复选框,以隐藏或显示嵌套主题

显然,正如您的原始代码所示,$tests数组将由数据库查询的结果填充

<!DOCTYPE html>
<html>
  <head>
    <title>Medical Stuff</title>
  </head>
  <body>
    <h1>Medical Stuff</h1>

    <?php
      $tests = array(
        "Vital Signs",
        "Neuro-Psychological",
        "Laboratory",
        "Radiology",
        "Ultrasound",
        "Audiometry",
        "Optometry",
        "ECG",
        "Treadmill",
        "Dental",
        "Physical Examination",
        "Pediatrics",
        "MRI"
      );
      $topics = array(
        "Complete Blood Count",
        "Blood Typing",
        "Urinalysis",
        "RPR/TPHA",
        "Hepatitis B screening",
        "Fasting Blood Sugar",
        "Creatinine",
        "Total Cholesterol(Low Cholesterol, High Cholesterol)",
        "Triglyceride",
        "VLDL",
        "Blood Uric Acid",
        "Anti-HAV Igm Screening",
        "Anti HBaAg",
        "Drug & Alcohol Test",
        "Stool Culture"
      );
      $tests_length = count($tests);
      $topics_length = count($topics);
    ?>

    <form>
      <fieldset>
        <legend>Tests</legend>

        <?php
          // Tests
          for ($i = 0; $i < $tests_length; $i++) {

            $test_value = $i + 1;
            $test_id = "test_" . $test_value;

            print '<div>';
            print '<input type="checkbox" class="test" id="' . $test_id . '" value="' . $test_value . '">';
            print '<label for="' . $test_id . '">' . $tests[$i] . '</label>';

            // Nested topics
            print '<fieldset class="topics">';

            for ($ii = 0; $ii < $topics_length; $ii++) {

              $topic_value = $ii + 1;
              $topic_id = $test_id . "_topic_" . $topic_value;

              print '<div>';
              print '<input type="checkbox" class="topic" id="' . $topic_id . '" value="' . $topic_value . '">';
              print '<label for="' . $topic_id . '">' . $topics[$ii] . '</label>';
              print '</div>';
            }

            print '</fieldset>';
            print '</div>';
          }
        ?>

      </fieldset>

      <input type="submit" value="Submit">
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
      jQuery(document).ready(function($){

        $(".topics").hide();

        $(".test").on('click', function(e){
          var topics = $(this).nextAll('.topics');

          if ($(this).is(':checked')) {
            topics.show();
          } else {
            topics.hide();
          }
        });

      });
    </script>
  </body>
</html>

医疗用品
医疗用品
测验
jQuery(文档).ready(函数($){
$(“.topics”).hide();
$(“.test”)。在('click',函数(e)上{
var topics=$(this.nextAll('.topics');
如果($(this).is(':checked')){
topics.show();
}否则{
topics.hide();
}
});
});
上面的代码最终如下所示:


在我看来,最简单的方法是预加载所有复选框,隐藏它们,然后根据需要使用一些javascript显示它们

我将向您介绍如何使用javascript实现这一点

下面是javascript和附带html的基本思想

Html

以及指向一个工作示例的链接。

如果需要的话,我很乐意进一步解释。如果这需要使用多组子复选框,我也很乐意向您展示这是如何工作的


编辑:这是使用与MarkPlewis完全相同的技术,如果您需要帮助php如何生成我上面编写的html,请告诉我。

Vista-要确认,您需要选中实验室复选框,然后其他复选框显示在下面?如果可能,请在实验室下面选择“是”:(好的,这样做的方法是使用javascript,你说你认为javascript不起作用?你试过了吗?还是你不想使用它?javascript将是唯一一种不用重新加载页面就能让它起作用的方法。或者你预加载子复选框,然后根据用户选择隐藏/显示,或者用ajax单击加载,当然这两者都是通过JSI,我尝试了它,正如您在我的php代码中看到的,它是数组的,这很好,但我宁愿保留我的面向对象的php代码,我的意思是它是数组的,即name=“test[]”基于上面的帖子,但实际上你的工作很棒,我可能会将其用于我的其他工作和项目。这很好,但正如你所看到的,我的主要复选框是绑定:($d1='.$mtn.
”;echo$d1;抱歉,我不确定您所说的绑定是什么意思?正如您在我上面的php代码中所看到的,我的主复选框已合并到1I中。我们可以更改php吗?您可以在问题中添加更多的php代码,以便我可以看到到底发生了什么?