如何使用foreach表中的ajax获取另一个值

时间:2018-02-14 06:54:27

标签: php ajax codeigniter

我有来自foreach的表,在这个表中我使用ajax创建提交(id =" detail"),但为什么当我提交时我得到第一行,当我点击第二行值时,我得到第一行值不是第二行值?

<form action="<?php echo base_url() ?>index.php/stock_opname/proses_detail" method="post">
        <table class="table table-striped">
            <thead>
            <tr>
                <th>No</th>
                <th>SO No</th>
                <th>Opname Date</th>
                <th>Warehouse</th>
                <th>Type</th>
                <th>Approve</th>
                <th>Close period</th>
                <th>Detail</th>
            </tr>
            </thead>
            <tbody>
            <?php $no=1; foreach ($show as $key) { ?>
            <tr>
                    <td><?php echo $no ?></td>
                    <td><input type="text" id="sono" value="<?php echo $key->sono ?>"></td>
                    <td><input type="text" id="opnamedate" value="<?php echo $key->opnamedate ?>"></td>
                    <td><input type="text" id="warehousecode" value="<?php echo $key->warehousecode ?>"></td>
                    <td><input type="text" id="stocktypeid" value="<?php echo $key->stocktypeid ?>"></td>
                    <td></td>
                    <td></td>
                    <td><a href="#" id="detail" class="btn btn-info">Detail</a></td>
            </tr>
            <?php $no++;} ?>
            </tbody>
        </table>
    </form>


<script type="text/javascript">
    //Ajax Load data from ajax
    $(document).on('click', '#detail', function(){

            var sono = $('#sono').val();
            var opnamedate = $('#opnamedate').val();
            var wh = $('#warehousecode').val();
            var stocktypeid = $('#stocktypeid').val();

            alert(sono);

        });

3 个答案:

答案 0 :(得分:1)

你必须使id也是动态的,脚本sholud也是动态的。希望你能得到它

id =“detail1”

.on('click','#detail1',function()----首先 .on('click','#detail2',function()---- for second等等

答案 1 :(得分:0)

你能试试吗?

$('#detail').click(function(){
    var sono = $(this).siblings('#sono').val();
    var opnamedate = $(this).siblings('#opnamedate ').val();
    var wh = $(this).siblings('#wh').val();
    var stocktypeid = $(this).siblings('#stocktypeid ').val();

    alert(sono);
});

答案 2 :(得分:0)

<form action="<?php echo base_url() ?>index.php/stock_opname/proses_detail" method="post">
    <table class="table table-striped">
        <thead>
        <tr>
            <th>No</th>
            <th>SO No</th>
            <th>Opname Date</th>
            <th>Warehouse</th>
            <th>Type</th>
            <th>Approve</th>
            <th>Close period</th>
            <th>Detail</th>
        </tr>
        </thead>
        <tbody>
        <?php $no=1; foreach ($show as $key) { ?>
        <tr>
                <td><?php echo $no ?></td>
                <td><input type="text" id="sono<?php echo $no; ?>" value="<?php echo $key->sono ?>"></td>
                <td><input type="text" id="opnamedate<?php echo $no; ?>" value="<?php echo $key->opnamedate ?>"></td>
                <td><input type="text" id="warehousecode<?php echo $no; ?>" value="<?php echo $key->warehousecode ?>"></td>
                <td><input type="text" id="stocktypeid<?php echo $no; ?>" value="<?php echo $key->stocktypeid ?>"></td>
                <td></td>
                <td></td>
                <td><a href="#" id="detail" onclick="getDetails(<?php echo $no; ?>)" class="btn btn-info">Detail</a></td>
        </tr>
        <?php $no++;} ?>
        </tbody>
    </table>
</form>


<script type="text/javascript"> //Ajax Load data from ajax
function getDetails(no){
    var sono = $('#sono'+no).val();
        var opnamedate = $('#opnamedate'+no).val();
        var wh = $('#warehousecode'+no).val();
        var stocktypeid = $('#stocktypeid'+no).val();
        alert(sono);}

希望这会有所帮助