gpt4 book ai didi

javascript - 如何在 Javascript 中插入带有 HTML 标记的 PHP 代码

转载 作者:行者123 更新时间:2023-11-30 20:11:00 25 4
gpt4 key购买 nike

我想在 javascript 中添加一个带有 HTML 标签的 PHP 脚本。下面你可以看到我试过的代码。但是它不工作并指出有错误

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

这是我得到的错误:

enter image description here

最佳答案

你需要多加一个'echo " 之后否则你得不到所需的 javascript 字符串,如 '<option... >'但是你得到一个<option... >这在 js 代码中没有任何意义。

 echo "'<option value='" . $product->product_id . "'>'" 

关于javascript - 如何在 Javascript 中插入带有 HTML 标记的 PHP 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52418202/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com