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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失