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 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!