douren2395 2015-01-08 17:44
浏览 35
已采纳

Ajax回调仅适用于jquery表单组列表中的顶部div

Hello smarter people,

So as the title conveys, i'm having some difficulties, and I've wasted a good 8 hours trying to figure it out :)

I posted a Question earlier, to which I got no answers. I was having difficulties with my ajax, and it's interaction with a form. This form is in an admin panel, and is for dynamically creating and editing navigation links on the front end of my (hopefully) soon-too-be site.

My problem specifically is: I am unable to, via ajax, have the text-content of the anchor tag (which causes the form to dropdown), to immediately reflect on screen...

http://i1379.photobucket.com/albums/ah126/conchairtoe/3_zpsb897a6a4.png

As you can see, the ajax has run, no errors, yet the label (anchor tag for enacting drop-down form) remains "test", when I typed "testing!!!!!!" in.

Everything behind the scenes worked, as the database successfully updates the corresponding data, and so does the anchor tag text once i reload the page, but that ain't very ajaxy now is it!

Here's the Javascript for when the form is submitted.

// NAV ITEM FORM - UPDATE / SUBMIT      
    $(".nav-form").submit(function(event) {

        var navData = $(this).serializeArray(); // The serializeArray( ) method serializes all forms and form elements like the .serialize() method but returns a JSON data structure for you to work with.
        var navLabel = $('input[name=label]').val();        
        var navID = $('input[name=id]').val();

        $.ajax({

            url: "ajax/navigation.php",
            type: "POST",
            data: navData

        }).done(function(){

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

        }); // END $.ajax

        alert("navID = "+navID+"  navLabel = "+navLabel);

        event.preventDefault(); // will prevent form from submitting

    });//END nav-form

Please note the .done callback.

I've narrowed the problem down!

The ajax works ONLY on whichever of the sortable anchors is in position 1 (the top of the list) - for when I change the "Home Page", or, again, which ever one I happen to move to the top of the list, the label updates immediately as per the ajax.

As you can see in the code, I have those alerts to verify this: http://i1379.photobucket.com/albums/ah126/conchairtoe/final_zps7a7ddc68.png

Here is the code for the DOM environment

Navigation

<div class="row">

<div class="col-md-3">

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

<!--EXISTING NAV-ITEMS-->           
        <?php

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

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

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

                    <a id="label_<?php echo $list['id']; ?>" data-toggle="collapse" data-target="#form_<?php echo $list['id']; ?>">

                        <?php echo $list['label']; ?> <i class="fa fa-chevron-down"></i>

                    </a>                        

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

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

                            <!-- ID  -->                    
                            <div class="form-group">

                                <label class="col-sm-2 control-label" for="id">ID</label>

                                <div class="col-sm-10">
                                    <input type="text" class="form-control input-sm" value="<?php echo $list['id'];?>" name="id" id="id" placeholder="ID - name" />                                         
                                </div>

                            </div>

                            <!-- LABEL -->
                            <div class="form-group">

                                <label class="col-sm-2 control-label" for="label">Label</label>

                                <div class="col-sm-10">
                                    <input type="text" class="form-control input-sm" value="<?php echo $list['label'];?>" name="label" id="label" placeholder="Label" />
                                </div>

                            </div>

                        <!--URL -->                 
                            <div class="form-group">

                                <label class="col-sm-2 control-label" for="value">URL</label>

                                <div class="col-sm-10">
                                    <input class="form-control input-sm" type="text" value="<?php echo  $list['url'];?>" name="url" id="url" placeholder="URL"></input>
                                </div>

                            </div>

                            <!--POSITION -->                    
                            <div class="form-group">

                                <label class="col-sm-2 control-label" for="value">Position</label>

                                <div class="col-sm-10">
                                    <input class="form-control input-sm" type="text" value="<?php echo  $list['position'];?>" name="position" id="position" placeholder="Position"></input>
                                </div>

                            </div>

                            <!-- STATUS -->
                            <div class="form-group">

                                <label class="col-sm-2 control-label" for="status">Status</label>

                                <div class="col-sm-10">
                                    <input class="form-control input-sm" type="text" value="<?php echo  $list['status'];?>" name="status" id="status" placeholder=""></input>
                                </div>

                            </div>

                            <!--Button and Hidden Input-->                  
                            <button type="submit" class="btn-btn-default">Save</button>
                            <input type="hidden" name="submitted" value="1" />
                            <input type="hidden" name="opened_id" value="<?php echo $list['id']; ?>" />

                            <span class="pull-right">
                                <a id="del_<?php echo $list['id']; ?>" class="btn btn-danger nav-item-delete" href="#" style="margin-bottom: 2px;" title="delete nav item"><i class="fa fa-trash"/></i></a>
                            </span>

                        </form>  <!--END form-->

                    </div>

                </li>

            <?php } ?>      

    </ul>

</div>

<div class="col-md-9">

    <div id="callback"><?php if(isset($message)) { echo $message; } ?></div>

</div>  <!-- END col-md-12 -->

So yea, I truly apologize for my ignorance. Bare in mind that I barely could grasp css just 2 months ago or so.

THANK YOU SO MUCH FOR YOUR HELP IF YOU CAN PROVIDE IT.

  • 写回答

1条回答 默认 最新

  • doujiu3097 2015-01-08 17:49
    关注

    Because you are not looking at the current form for the navID. The code $('input[name=id]') is using the first element it finds. You need to look at the form, use find().

    var form = $(this);
    var navData = form.serializeArray(); 
    var navLabel = form.find('input[name=label]').val();        
    var navID = form.find('input[name=id]').val();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加