2017-05-28 118 views
-2

我在互聯網上找到了更多的解決方案,但沒有收到我的案例!將innerHTML從php轉換爲javascript

我有一個foreach生成選擇使用PHP構建的選項,我想在我的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

哪裏是jQuery代碼?你想在jQuery中使用PHP嗎? –

+0

問題已更新,我將在此處使用** divtest.innerHTML ** – Syam

+0

更好地使用'ajax' – Ukasyah

回答

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> 
+0

謝謝,但是爲什麼class selectpicker在InnerHtml中禁用並且在其他地方工作 – Syam

+0

我不明白你在問什麼 –

+0

Class selectpicker在'