猿问

我无法保存特定的选定值。它保存了数据库中的所有数据

我正在插入多个数据。当我从下拉列表中选择一个项目时,会发生什么。它会保存所有选择的数据。请先检查下面的图片


    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());

    });

这是我保存时发生的情况。

一只名叫tom的猫
浏览 181回答 1
1回答

天涯尽头无女友

之所以这样保存,是因为您的select元素只有一个选项值修改自:<td class="pro"> <select class="pro"> <option value=""><?php sample($con); ?></option> </select> </td>至:<td class="pro">&nbsp; &nbsp; <select class="pro">&nbsp; &nbsp; &nbsp; &nbsp; <option value=""></option>&nbsp; &nbsp; &nbsp; &nbsp; <?php sample($con); ?>&nbsp; &nbsp; </select>&nbsp;</td>并修改您的选择元素处理程序以仅推送您选择的项目像这样的东西:$('.pro').each(function(){&nbsp;&nbsp; &nbsp; if($(this).prop("selected")) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; pro.push($(this).text());&nbsp;&nbsp; &nbsp; }});或将此部分修改为:$(".pro > option:selected").each(function(){&nbsp; &nbsp; pro.push($(this).text());});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答