douruoshen1449 2018-03-04 08:37
浏览 77
已采纳

表单内部while循环只通过ajax发送第一行数据

I have a form inside a while loop which I want to use to update the database using ajax. The problem is that when I am using ID only the 1st data or 1st row data of the form is sent no matter which button I click. When I change the IDs to CLASSES the console shows undefined.

Error in Console when using classes:

Object { credits: undefined, price: undefined, packId: undefined, type: "savePackage" }

HTML Form:

<div class="content">
        <div class="content table-responsive table-full-width">
          <table class="table table-hover table-striped">
            <tr>
              <th>Credits</th>
              <th>Price</th>
              <th>Action</th>
            </tr>
            <?php while($pack = $package->fetch()){ extract($pack); ?>
              <tr>
                <form method="post" action="">
                  <input type="hidden" value="<?php echo $cp_id; ?>" class="packId">
                  <td><input type="text" class="form-control" class="editCredits" value="<?php echo $cp_credits; ?>"></td>
                  <td><input type="text" class="form-control" class="editPrice" value="<?php echo $cp_price; ?>"></td>
                  <td>
                    <input type="submit" value="Save" class="btn btn-fill btn-info savePackage">
                    <input type="submit" value="Delete" class="btn btn-fill btn-danger deletePackage">
                  </td>
                </form>
              </tr>
            <?php } ?>
          </table>
        </div>
      </div>

AJAX Code

$(document).ready(function() {
  $(".savePackage").click(function() {
    var dataString = {
      credits: $(this).closest('form').find('.editCredits').val(),
      price: $(this).closest('form').find('.editPrice').val(),
      packId: $(this).closest('form').find('.packId').val(),
      type: 'savePackage'
    };
    console.log(dataString);
    var $submit = $(this).parent().find('.savePackage');
    $.confirm({
      title: 'Confirm!',
      content: 'Are you sure you want to add this package?',
      buttons: {
        confirm: function () {
          $.ajax({
            type: "POST",
            //dataType : "json",
            url: "ptc-settings-process.php",
            data: dataString,
            cache: true,
            beforeSend: function(){
              $submit.val("Please wait...");
            },
            success: function(html){
              $.alert(html);
              $submit.val("Save");
            }
          });
        },
        cancel: function(){}
      }
    });
    return false;
  });
});

I have used $(this).closest('form').find('.editCredits').val() method here but there are multiple other forms on the same page too. Therefore, maybe using form element in $(this).closest('form') could be causing a problem.

  • 写回答

1条回答 默认 最新

  • douchi8503 2018-03-04 09:10
    关注

    Inside your $(".savePackage").click, the form you are looking for with $(this).closest('form') is not found.

    I think that is because you have your form inside the table structure after the <tr>.

    What you could do is move your form to before the <table> definition:

    <form method="post" action="">
      <table class="table table-hover table-striped">
    

    You also have a duplicate class definition class="form-control" class="editCredits" in your <input>.

    You could update that to class="form-control editCredits"

    Instead of using multiple forms, and find the form you could find the closest <tr> and then find the <td> with the input and the class.

    Your code might then look like:

    $(document).ready(function() {
      $(".savePackage").click(function() {
        var dataString = {
          credits: $(this).closest('tr').find('td input.editCredits').val(),
          price: $(this).closest('tr').find('td input.editPrice').val(),
          packId: $(this).closest('tr').find('.packId').val(),
          type: 'savePackage'
        };
        console.log(dataString);
        var $submit = $(this).parent().find('.savePackage');
        $.confirm({
          title: 'Confirm!',
          content: 'Are you sure you want to add this package?',
          buttons: {
            confirm: function() {
              $.ajax({
                type: "POST",
                //dataType : "json",
                url: "ptc-settings-process.php",
                data: dataString,
                cache: true,
                beforeSend: function() {
                  $submit.val("Please wait...");
                },
                success: function(html) {
                  $.alert(html);
                  $submit.val("Save");
                }
              });
            },
            cancel: function() {}
          }
        });
        return false;
      });
    });
    <div class="content">
      <div class="content table-responsive table-full-width">
        <form method="post" action="">
          <table class="table table-hover table-striped">
            <tr>
              <th>Credits</th>
              <th>Price</th>
              <th>Action</th>
            </tr>
            <?php while($pack = $package->fetch()){ extract($pack); ?>
            <tr>
              <input type="hidden" value="<?php echo $cp_id; ?>" class="packId">
              <td><input type="text" class="form-control editCredits" value="<?php echo $cp_credits; ?>">
              </td>
              <td><input type="text" class="form-control editPrice" value="<?php echo $cp_price; ?>"></td>
              <td>
                <input type="submit" value="Save" class="btn btn-fill btn-info savePackage">
                <input type="submit" value="Delete" class="btn btn-fill btn-danger deletePackage">
              </td>
            </tr>
            <?php } ?>
          </table>
        </form>
      </div>
    </div>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程