在innerhtml中从php转换为javascript

时间:2017-05-28 06:22:43

标签: php jquery html

我在互联网上找到了更多解决方案,但我没有像我的情况那样得到任何东西!

我有一个 foreach 生成一个用PHP构建的Select选项,我想在我的jQuery上使用PHP到innerHtml。

PHP

<select class="selectpicker" data-show-subtext="true" data-live-search="true" id="select_product" name="select_product[]"">
  <?php
       foreach($order as $row):
        echo"<option value ='$row->p_id'>".$row->p_name. "</option>";
         endforeach;
           ?>
  </select>

我试图将该代码复制到JS变量中并调用它但不起作用

InnerHtml=?

当前脚本

<script >

var room = 1;
function education_fields() {

    room++;
    var objTo = document.getElementById('education_fields')
    var divtest = document.createElement("div");
    divtest.setAttribute("class", "form-group removeclass"+room);
    var rdiv = 'removeclass'+room;
    divtest.innerHTML = '<div class="col-sm-3 nopadding"><div class="form-group"> <input type="text" class="form-control" id="select_product" name="select_product[]" value="" placeholder="product"></div></div><div class="col-sm-3 nopadding"><div class="form-group"> <input type="text" class="form-control" id="Major" name="Major[]" value="" placeholder="Major"></div></div><div class="col-sm-3 nopadding"><div class="form-group"> <input type="text" class="form-control" id="Degree" name="Degree[]" value="" placeholder="Degree"></div></div><div class="col-sm-3 nopadding"><div class="form-group"><div class="input-group"> <select class="form-control" id="educationDate" name="educationDate[]"><option value="">Date</option><option value="2015">2015</option><option value="2016">2016</option><option value="2017">2017</option><option value="2018">2018</option> </select><div class="input-group-btn"> <button class="btn btn-danger" type="button" onclick="remove_education_fields('+ room +');"> <span class="glyphicon glyphicon-minus" aria-hidden="true"></span> </button></div></div></div></div><div class="clear"></div>';

    objTo.appendChild(divtest)
}
function remove_education_fields(rid) {
    $('.removeclass'+rid).remove();
}
</script>

1 个答案:

答案 0 :(得分:1)

尝试在脚本中添加此内容

<select class="selectpicker" data-show-subtext="true" data-live-search="true" id="select_product" name="select_product[]"">
                            <?php  foreach($order as $row){ ?>
                                <option value ="<?php echo $row->p_id; ?>"><?php echo $row->p_name; ?></option>
                            <?php }  ?>
                        </select>