javascript - 通过单击带有 js 的按钮添加 div 不能与 PHP 一起使用
问题描述
一旦我添加 PHP,Javascript 代码就不起作用
我有一个 javascript 函数,它通过单击按钮添加 div,代码在没有 PHP 的情况下完美运行,但只要放入以下 PHP 代码,它就不再工作了。请帮忙,谢谢!
<script>$(document).ready(function(){
var i=1;
$('#add').click(function(){
i++;
$('#dynamic_field').append('<tr id="row'+i+'"><td>Kategorie<br><select>"<?php while($row = mysqli_fetch_array($result)){echo '<option name="Kategoriename">' .$row['Kategorienname']. "</option>"; }?>"</select></td></select></td><td>Name<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Inhalt<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>normal<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>mittel<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>groß<input type="text" name="name[]" placeholder="Ø" class="form-control name_list" /></td><td>Preis<input type="text" name="name[]" placeholder="Enter your Name" class="form-control name_list" /></td><td>Allergene<br><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/a.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/b.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/c.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/d.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/e.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/f.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/g.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/h.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/l.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/m.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/n.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/o.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/p.png"><input type="checkbox" name="leer" value="leer"><img style="height:20px; width:20px;" src="../AuswahlIcon/allergene/r.png"></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
});
$(document).on('click', '.btn_remove', function(){
var button_id = $(this).attr("id");
$('#row'+button_id+'').remove();
});
$('#submit').click(function(){
$.ajax({
url:"name2.php",
method:"POST",
data:$('#add_name').serialize(),
success:function(data)
{
alert(data);
$('#add_name')[0].reset();
}
});
});
});
解决方案
这看起来不错,只是您在双引号内运行 PHP。以我的经验,这是行不通的。扩展您的 PHP 标签。
从:
$('#dynamic_field').append('<tr id="row'+i+'"><td>Kategorie<br><select>"<?php while
类似于:
$('#dynamic_field').append(<?php echo "<tr id=\"row'+i+'\"><td>Kategorie<br><select>"; while...
推荐阅读
- safari - Safari - createMediaElementSource 不起作用?
- php - 连接到 mongodb 时的权限问题
- javascript - 在 Firebase 云函数中使用导入
- javascript - 如何从自己的 URL 中找到网站 IP?
- python - 添加可见 gpu 设备时遇到问题:0
- javascript - 通过 Ajax 将数据从 Laravel 7 视图传递到控制器
- hdfs - 带有 HDFS 工件存储的 MLFLOW
- javascript - 如何修复无法在角度材料数据表中设置未定义的属性“排序”?
- c# - 创建一个将返回 JSON 数据而不更改当前 ASP.NET MVC 登录机制的新登录名
- python - 如何从正则表达式中排除两个单词?