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.

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

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。