我正在插入多个数据。当我从下拉列表中选择一个项目时,会发生什么。它会保存所有选择的数据。请先检查下面的图片 function sample($con){ $select = "SELECT * FROM project_tbl"; $select_result = mysqli_query($con,$select); if (mysqli_num_rows($select_result)> 0) { while ($row = mysqli_fetch_assoc($select_result)) { echo "<option value=".$row['project_name'].">" .$row['project_name']."</option>"; } } }* html代码(我使用下拉列表填充数据库中的数据)<td class="pro"> <select class="pro"> <option value=""><?php sample($con); ?></option> </select> </td>* ajax代码(添加,保存和删除)$(document).ready(function(){ var count = 1; $('#add').click(function(){ count = count + 1; var html_code = "<tr id='row"+count+"'>"; html_code += "<td class='pro'><select class='pro'><option value=''><?php sample($con); ?></option></select></td>"; html_code += "<td contenteditable='true' class='desc'></td>"; html_code += "<td contenteditable='true' class='comp'></td>"; html_code += "<td contenteditable='true' class='entry'></td>"; html_code += "<td contenteditable='true' class='remarks'></td>"; html_code += "<td><button type='button' name='remove' data-row='row"+count+"' class='btn btn-outline-danger btn-xs remove' title='remove'><i class='fa fa-times' aria-hidden='true'></i></button></td>"; html_code += "</tr>"; $('#crud_table').append(html_code); }); $(document).on('click', '.remove', function(){ var delete_row = $(this).data("row"); $('#' + delete_row).remove(); }); $('#save').click(function(){ var desc = []; var pro = []; var comp = []; var entry = []; var remarks = []; $('.desc').each(function(){ desc.push($(this).text()); }); $('.pro').each(function(){ pro.push($(this).text()); }); $('.comp').each(function(){ comp.push($(this).text()); }); $('.entry').each(function(){ entry.push($(this).text()); }); $('.remarks').each(function(){ remarks.push($(this).text()); });这是我保存时发生的情况。
1 回答
天涯尽头无女友
TA贡献1831条经验 获得超9个赞
之所以这样保存,是因为您的select元素只有一个选项值
修改自:
<td class="pro"> <select class="pro"> <option value=""><?php sample($con); ?></option> </select> </td>
至:
<td class="pro">
<select class="pro">
<option value=""></option>
<?php sample($con); ?>
</select>
</td>
并修改您的选择元素处理程序以仅推送您选择的项目
像这样的东西:
$('.pro').each(function(){
if($(this).prop("selected")) {
pro.push($(this).text());
}
});
或将此部分修改为:
$(".pro > option:selected").each(function(){
pro.push($(this).text());
});
添加回答
举报
0/150
提交
取消