【问题标题】:Ajax post data while loop phpAjax在循环php时发布数据
【发布时间】:2015-04-29 08:59:28
【问题描述】:

对不起,我是初学者。 我对来自 while 循环 PHP 的 ajax 发布数据有一些问题。 这是我的代码。

假设在while循环之后我有5条记录5个按钮,每条记录有不同的发布数据,但是无论我点击这5条记录中的哪个按钮,ajax都只会发送记录的第一条数据,ajax将发送第一条记录数据的相同数据

但是,如果我使用 $(this).val() ajax 将在每条记录中发送正确的数据。

请帮忙。非常感谢。

在 PHP 中。

<?php while($rs = mysql_fetch_array($qr)) { ?>
        <td width="150">
             <input type="text" name="studentid" class="studentid" value="<?=$rs['studentid']?>"/>
      </td>
    <td width="100">
        <select name="ssize" class="ssize">
            <option value="">ไซต์</option>
            <option <?php if($rs['ssize'] == 'S') { ?> selected="selected" <?php } ?> value="S">S</option>
            <option <?php if($rs['ssize'] == 'M') { ?> selected="selected" <?php } ?> value="M">M</option>
        </select>
    </td>
    <button value="<?=$rs['id']?>" class="printBill btn btn-primary">
            <i class="glyphicon glyphicon-duplicate"> </i>
            พิมพ์ใบเสร็จ
    </button>
<?php } ?>

在 JS 中。

$(".printBill").click(function () {
            $.ajax({
            url: "admin_search_save.php",
            data: {
                id:$(this).val(),
                studentid:$(".studentid").val(),
                ssize:$(".ssize").val(),
            },
            success: function (data) {
            }
        });
    });

【问题讨论】:

  • 你的问题能具体一点吗,比如你到底在找什么
  • @Teerpong Pothhiphun ,你能把整个桌子的结构贴出来,然后我们可以看到元素的位置......

标签: javascript php jquery ajax


【解决方案1】:

您需要使用event delegation 来动态添加内容。

$(document).on('click','.printBill',function () {

        // capture input and select value by traversing up into parent(tr),
        // then find element [input, select]
        // this traversing method depend on your table structure
        var studID    = $(this).closest('tr').find('input[name=studentid]').val(),
            ssizeData = $(this).closest('tr').find('select[name=ssize]').val();

        $.ajax({
        url: "admin_search_save.php",
        data: {
            id        : $(this).val(),
            studentid : studID,
            ssize     : ssizeData,
        },
        success: function (data) {
        }
    });
  });

【讨论】:

  • 他们有多个学生编号和多个尺寸。 JavaScript 可以全部通过吗?一定有更多事情需要处理。
  • @AkiEru :添加输入和选择的值。由于按钮本身在while循环中,因此无需发送多个值,只需捕获同一行的输入和选择元素即可。
猜你喜欢
  • 2017-04-20
  • 1970-01-01
  • 2014-03-10
  • 2021-02-24
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多