Javascript Ajax在循环php时发布数据

Javascript Ajax在循环php时发布数据,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,对不起,我是初学者。 我对while循环PHP中的ajax post数据有一些问题。 这是我的密码 假设在while循环之后,我有5条记录,5个按钮,每个记录都有不同的post数据,但是ajax将只发送记录的第一个数据,无论我单击这5条记录中的哪个按钮,ajax都将发送第一个记录数据的相同数据 但是,如果我使用$(this).val()ajax将在每个记录中发送正确的数据 请帮忙。太多了 在PHP中 <?php while($rs = mysql_fetch_array($qr)) { ?

对不起,我是初学者。 我对while循环PHP中的ajax post数据有一些问题。 这是我的密码

假设在while循环之后,我有5条记录,5个按钮,每个记录都有不同的post数据,但是ajax将只发送记录的第一个数据,无论我单击这5条记录中的哪个按钮,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 } ?>
您需要为动态添加的内容使用

$(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) {
        }
    });
  });

你能更具体地回答你的问题吗,比如你到底在看什么for@TeerpongPhothiphun,你能发布整个表格结构,然后我们可以看到元素的位置……它们有多个studentid和多个ssize。JavaScript能通过所有这些测试吗?一定还有别的事情要处理。@AkiEru:添加了输入值并选择。不需要发送多个值,因为按钮本身在while循环中,只需要捕获输入并选择同一行上的元素。
$(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) {
        }
    });
  });