drgdn82648 2016-05-30 20:30
浏览 27
已采纳

Ajax仅适用于提交的第一个id(使用来自db的php)

I'm developing an editor for navigation bar , to can reorder them , edit them and things like this. I made the part of sortable in ajax , but I got stuck at the submit function .

I want to change the name-viewed immediatly when i press the save button , and it works! , but only for my first placed navbar . At the second and third (in this example) , ajax won't work only If i replace them into the position one . maybe I can get some help from you guys , thanks in advance

Down here I will provide you my relevant codes for this part , and a screenshot at the end

ajax code

$('.nav-form').submit(function(event){

        var navData = $(this).serializeArray();
        var navLabel = $('input[name=label]').val();
        var navID = $('input[name=id]').val();

        $.ajax({

            url: "ajax/nav-form.php",
            type: "POST",
            data: navData

        }).done(function(){

            $("#label_"+navID).html(navLabel);

        });

calling the alert method for navLabel and navID , i observed that they call only the first positioned item (even if i'm trying to edit the second or third navbar) , doesn't matter which one is it , if it's first placed

        alert(navLabel);
        alert(navID);

        event.preventDefault();

    });    



}); // END document.ready();

the relevant php/html codes

<ul id="sort-nav" class="list-group">

        <?php

        $q = "SELECT * FROM navigation ORDER BY position ASC";
        $r = mysqli_query($dbc, $q);

        while ($list = mysqli_fetch_assoc($r)) { ?>

            <li id="list_<?php echo $list['id']; ?>" class="list-group-item">

Form here i call the id in ajax

                <a id="label_<?php echo $list['id']; ?>">
                    <?php echo $list['label']; ?>
                </a>

                <button class="btn btn-success pull-right" data-toggle="collapse" data-target="#form_<?php echo $list['id']; ?>"><i class="fa fa-chevron-down"></i></button>

                <div id="form_<?php echo $list['id']; ?>" class="collapse">


                    <form class="form-horizontal nav-form" action="index.php?page=navigation&id=<?php echo $list['id']; ?>" method="post" role="form">



this part of code I've erased because it's irelevant

<button type="submit" class="btn btn-default">Save</button>
                        <input type="hidden" name="submitted" value="1">

                        <input type="hidden" name="openedid" value="<?php echo $list['id']; ?>">

                    </form>
                </div>

            </li>
        <?php } ?>

enter image description here hope I dind't gave you headaches with my bad english

  • 写回答

1条回答 默认 最新

  • dongwen2162 2016-05-30 20:37
    关注

    Delegate the submit event so event listener isn't lost when the element gets moved

    $(document).on('submit','.nav-form',function(event){
         // your same existing code
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集