dsfdfdfd6576578 2018-09-20 05:32
浏览 73
已采纳

如何在Javascript中插入包含HTML标记的PHP代码

I want to add a PHP script which has HTML tags, inside a javascript. Below you can see the code I tried. But It's not working and stating that there is an error

<script type='text/javascript'>
//<![CDATA[
 $(document).ready(function() {
  var currentItem = 11;
  $('#addnew').click(function(){
   currentItem++;
   $('#items').val(currentItem);
var strToAdd = '<tr><td><select class="form-control select2" name="product'+currentItem+'" id="product'+currentItem+'" style="width: 100%">'+
                <?php foreach ($productData as $product) { echo "<option value='" . $product->product_id . "'>" . $product->product_name . "</option>"; }?>+
                '</select> </td><td><input class="form-control" name="quantity'+currentItem+'" id ="quantity'+currentItem+'"type="text" /></td> 
                <td><input class="form-control"  name="freeIssue'+currentItem+'" id="freeIssue'+currentItem+'" type="text" /></td>
                 <td align="center"><button  class="btn btn-danger" name="close" id="close" onclick="SomeDeleteRowFunction(this)"><i class="fa fa-close"></i></button></td></tr>';



   $('#data').append(strToAdd);

  });

  $('#remove').on("click", function(){
    $('#data tr:last').remove();
})

 });

//]]>
</script>

This is the error I get:

enter image description here

</div>
  • 写回答

1条回答 默认 最新

  • douyun1546 2018-09-20 06:35
    关注

    You need to put an extra ' after echo " otherwise you do not get the desired javascript string like '<option... >' but you get an <option... > which means nothing in a js code.

     echo "'<option value='" . $product->product_id . "'>'" 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?