dsjhejw3232 2017-04-26 10:01
浏览 15
已采纳

使用选择下拉列表使用Jquery更新表行

I am trying to update some table rows (there can be 40-50 rows) generated from a MYSQL query with a Approve/Rejected column at the end of each row, I would like to use jQuery to update the row using a select dropdown, the problem I am having is that it only updates the first row.

I have tried to look at other Stackoverflow threads, there seems to be quite a few with similar issues, but I am still trying to get my head around jQuery so I couldn't work out how to do it.

I am guessing it's because of the same class names?

My HTML table/forms

<table>
    <tr>
        <td>1</td>
        <td>Ren</td>
        <td>
        <form method="post" action="" class="dangermouse">
            <div class="form-group">
                <select name="approval" class="approval">
                    <option value="0">Approved</option>
                    <option value="1">Reject</option>
                </select>
            </div>
            <input type="hidden" name="id" value="1">
            <input type="button" name="updaterow" class="save_button" value="Update">
        </form>
        </td>
    </tr>
    <tr>
        <td>2</td>
        <td>Stimpy</td>
        <td>
        <form method="post" action="" class="dangermouse">
            <div class="form-group">
                <select name="approval" class="approval">
                    <option value="0">Approved</option>
                    <option value="1">Reject</option>
                </select>
            </div>
            <input type="hidden" name="id" value="2">
            <input type="button" name="updaterow" class="save_button" value="Update">
        </form>
        </td>
    </tr>
</table>

jQuery:

$('.save_button').click(function() {
    var approval = $('.approval').val();
    var id = $('.id').val();

    $('.save_status').text('loading...');

    $.post('updateRow.php',{
            approval: approval,
            id: id
        }, function(data) {
            $('.save_status').text(data);
        }
    );
});

PHP File:

if(isset($_POST['approval'],$_POST['id'])) {
$approval = $_POST['approval'];
$id = $_POST['id'];
if($approval !="" && $id !="") {
    $pdo->ApproveOrDeny($approval, $id);
    } else {
        echo "The same thing we do every night, Pinky - try to take over the world!";
    }
}

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dongye1143 2017-04-26 10:22
    关注

    You need to access the clicked form values .instead of just using class name

    1) Setup unique name for each form

    2) Access the value using form name like this $('form[name="form1"]>.id').val()

    $('.save_button').click(function() {
    
       var form = $(this).parents('form:first').attr('name');   
       
       alert(form);
    
       var id = $('form[name="'+form+'"]>.id').val();
       var option =$('form[name="'+form+'"] .approval').val();
    
       alert(id);
       alert(option);
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
        <tr>
            <td>1</td>
            <td>Ren</td>
            <td>
            <form method="post" name="form1" action="" class="dangermouse">
                <div class="form-group">
                    <select name="approval" class="approval">
                        <option value="0">Approved</option>
                        <option value="1">Reject</option>
                    </select>
                </div>
                <input type="hidden" class="id" value="1">
                <input type="button" name="updaterow" class="save_button" value="Update">
            </form>
            </td>
        </tr>
        <tr>
            <td>2</td>
            <td>Stimpy</td>
            <td>
            <form method="post" name="form2" class="dangermouse">
                <div class="form-group">
                    <select name="approval" class="approval">
                        <option value="0">Approved</option>
                        <option value="1">Reject</option>
                    </select>
                </div>
                <input type="hidden" class="id" value="2">
                <input type="button" name="updaterow" class="save_button" value="Update">
            </form>
            </td>
        </tr>
    </table>

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c