javascript - 克隆后无法从选择框中选择选项

标签 javascript jquery html

每次点击添加更多学生链接时,我都会克隆整个 div。

克隆可以工作,但无法从克隆 div 的克隆选择框中选择选项。

HTML

      <div id="all">
        <div id="student" class="row">
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="control-label">Firstname</label>
                    <label style="color:red;" id="std_first_name_error"></label>
                    <div class="append-icon">
                        <input type="text" id="std_first_name" name="std_first_name1" id="password" class="form-control" placeholder="Enter first name" minlength="4" maxlength="16" required>
                        <i class="icon-lock"></i>
                    </div>
                </div>
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label class="control-label">Select School</label>
                    <label style="color:red;" id="std_scl_error"></label>
                    <div class="option-group">
                      <select  name="std_scl_name1" class="language" required>


                        <option value="">Select school..</option>

                        <?php foreach ($schools as $school) :?>
                            <option value="<?php echo $school->sch_id; ?>"><?php echo $school->sch_name;?></option>
                        <?php endforeach; ?>

                      </select>
                    </div>
                  </div>
            </div>
        </div>
        </div>

        <div id="add_student"><a><u>Add More Students</u></a></div>

脚本

    <script type="text/javascript">

        $(document).ready(function(){       
        var count = 2;
        $('#add_student').click (function(e){
            e.preventDefault();
            var clonedEl = $('#student').first().clone();
            clonedEl.find(':text').attr('name','std_first_name'+count);
            //Add the newly div the the entire div
            $('#all').append(clonedEl);

            //$('[name="std_scl_name'+count+'"]').html($('[name="std_scl_name1"]').html());

          });
      });
        </script>

申请

enter image description here

最佳答案

您的相同代码在 this fiddle 中运行良好。我更改/添加的是唯一的<option>元素。我仍然可以从克隆的 div 的 <select> 中选择一个元素.

我认为你需要检查的是 php片段及其输出。它可能无法满足 <option>元素。

关于javascript - 克隆后无法从选择框中选择选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30413085/

相关文章:

javascript - html 元素通过鼠标或键盘获得焦点

javascript - 如何将 JSON 传递给 jqChart?

JavaScript - 获取所选表格行的第一个单元格的数据

javascript - Jquery 在图像问题下显示圆形标记图像

javascript - 使用链式查询从列表中选择

html - 如何为所有浏览器创建 css3 平滑过渡?

html - 居中行内 block 元素后的空白

javascript - plotOptions.series.point.events 在使用向下钻取时返回空点数据

javascript - jQuery ID 选择和动态数组

javascript - `xhr.getAllHeaders()` 在浏览器扩展上下文中缺少一些 header ?