javascript无法以动态形式运行

时间:2017-02-13 18:35:40

标签: javascript php jquery forms dynamic

我有一个动态表单,显示来自数据库的数据和一个javascript代码来显示和隐藏一些字段,但javascript代码仅适用于第一个结果 这是php代码

<?php
                $arr = select::mobiles($id);
                foreach ($arr as $aa) { ?>
                <div class="col-sm-10">
                    <div class="col-sm-2 bring_right">
                        <input type="hidden" name="p_name2[<?php $counter; ?>]"
                               value="<?php echo $aa['name']; ?>"><?php echo $aa['name'] ?>
                    </div>
                    <div class="col-sm-4 bring_right">
                        <input type="radio" id="general_radio" name="">
                        <label for="">general</label>
                        <input type="radio" onclick="showMe('variant', this)" id="variant_radio" name="">
                        <label for="">variant</label>
                    </div>
                    <div class="col-sm-6 bring_right" id="variant" style="display: none">
                        <?php
                        $branches = select::mobile_branches();
                        //                            print_r($branches);
                        foreach ($branches as $b) {
                            ?>
                            <div class="col-sm-4 bring_right"><label for=""><?php echo $b['name'] ?></label></div>
                            <div class="col-sm-8">
                                <select class="form-control input-sm"
                                        name="p_value[<?php echo $aa['name']; ?>][<?php $counter; ?>]">
                                    <?php
                                    $pid = select::property($aa['name']);
                                    $arr2 = select::properties($pid);
                                    foreach ($arr2 as $aa2) { ?>
                                        <option value="<?php echo $aa2['name'] ?>" style="direction:ltr">
                                            <?php echo $aa2['name'] ?></option>
                                    <?php }
                                    $counter++; ?>
                                </select>
                            </div>
                        <?php } ?>
                    </div>
                    <div class="col-sm-3 bring_right" id="general">
                        <select multiple class="form-control input-sm"
                                name="p_value[<?php echo $aa['name']; ?>][
                <?php $counter; ?>]">
                            <?php
                            $pid = select::property($aa['name']);
                            $arr2 = select::properties($pid);
                            foreach ($arr2 as $aa2) { ?>
                                <option value="
                <?php echo $aa2['name'] ?>">
                                    <?php echo $aa2['name'] ?></option>
                            <?php }
                            $counter++; ?>
                        </select>
                    </div>
                </div>
            <?php } ?>

并且javascript代码是

<script type="text/javascript">
function showMe(it, box) {
    var vis = (box.checked) ? "block" : "none";
    document.getElementById(it).style.display = vis;
    $('#general').hide();

}
</script>

任何帮助吗??

1 个答案:

答案 0 :(得分:1)

嘿我觉得我遇到了你的问题。在您的代码中,请参阅此部分

foreach ($arr as $aa) { ?>
                ......
<div class="col-sm-6 bring_right" id="variant" style="display: none">

此处您对多个div拥有相同的ID,只需尝试以下更改,您就可以执行此操作

foreach ($arr as $key => $aa) { ?>
                    ......
<input type="radio" onclick="showMe('variant<?php echo $key; ?>', this)" id="variant_radio" name="">
........
<div class="col-sm-6 bring_right" id="variant<?php echo $key; ?>" style="display: none">

如果我没有明白你的观点,或者它不起作用,请告诉我。