duanaixuan7385 2015-03-21 21:50
浏览 51
已采纳

Javascript只有第一个按钮提交到Ajax表单,尽管不同的ID

Other users have had similar problems about when they're looping rows of buttons but it's always because they accidentally reused the same Id or value. I'm experiencing the same problem, but all of my buttons are unique.

AJAX request

    <script>
        $(document).ready(function(){
            $("#friendadd").submit(function(){
                    alert("checkpoint");
                    $.ajax({
                        type:"POST",
                        url:"getuser.php"
                    });
            })
        });
    </script>

PHP and form

            <form id="friendadd">
    <?php
        for($i=0; $i<$ctk->rowCount(); $i++){
            echo "<img src='".$ctk_values[$i][6]."' alt='Blank' style='width:64px;height:64px'>";//PP, Later add clickable profile
            echo "<th rowspan='3'>Attributes</th>";
            echo "<tr>  ".$ctk_values[$i][0]."</tr>";//UN
            echo "<tr>  ".$ctk_values[$i][1]."</tr>";//UL
            echo "<tr>  ".$ctk_values[$i][5]."</tr>";//UA
    ?>
                <input type="submit" id="friend<?php echo $i;?>"><!--pass in this.value-->
            </form>
    <?php

                }//Ends for loop
            }
        }
    ?>

Explanation: When I type in a username into the search box, it returns me three different users named rikesh1, rikesh2, and rikesh3. Each of them have a button next to them, with values friend0, friend1, friend2, respectively. When I click on the friend0 button, it successfully calls and updates the database. When I click the friend1 button, nothing happens. This is different from other users in that my buttons have unique Ids. Thanks for any and all help, I think this is a very fixable problem but after searching Stack, I'm still not sure what's happening.

  • 写回答

1条回答 默认 最新

  • dongyan7950 2015-03-21 21:59
    关注
            <form id="friendadd">
    <?php
        for($i=0; $i<$ctk->rowCount(); $i++){
            echo "<img src='".$ctk_values[$i][6]."' alt='Blank' style='width:64px;height:64px'>";//PP, Later add clickable profile
            echo "<th rowspan='3'>Attributes</th>";
            echo "<tr>  ".$ctk_values[$i][0]."</tr>";//UN
            echo "<tr>  ".$ctk_values[$i][1]."</tr>";//UL
            echo "<tr>  ".$ctk_values[$i][5]."</tr>";//UA
    ?>
                <input type="submit" id="friend<?php echo $i;?>"><!--pass in this.value-->
    <?php
    
                }//Ends for loop
    ?>
            </form>
    <?php
            }
        }
    ?>
    

    Use this code instead.

    The other one ends the form tag at the first loop.

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

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题