-1

我试图在使用 jQuery.ajax 后更改跨度的值。

JavaScript

$(document).on('click', '.kssico', function(event) {
    var for_uid = $(this).parents("li").attr('data'),
        for_name = $(this).parents("li").attr('unme'),
        dataString = "for_uid=" + for_uid + "&for_name=" + for_name; 

    $.ajax({
        type: "POST",
        url: "include/ajax.php",
        data: dataString,
        success: function (html) {
            if(html=="300") {
                $('#myModal .modal-body p').html("Error Please Try Again.");
                $('#myModal').modal('show');
            }
            else {
                $(this).parents("li span.mi-val").html(html);
                $('#myModal .modal-body p').html("The Requested Action Has Been Notified To Your Friend.");
                $('#myModal').modal('show');
            }
        }
    });
});

HTML

<li class="mispan main-span" >
    <div class="thumbnail">
        <h3 class="miheader">Dewashree Singh</h3>
        <a >
            <div class="miprofile-pic-cnt" ></div>
        </a>
        <div class="caption">
            <p>
                <a href="#" class="btn kssico miclickks">
                    <img src="/lisps.png"><span class="mi-val">0</span>
                </a>
                <a href="#" class="btn bdico miclickbd">
                    <img src="/besd.png"><span class="mi-val">1</span>
                </a>
            </p>
        </div>
    </div>
</li>

当我单击a.miclickks它时,应该完成 Ajax 调用并返回一个值,该值也被放置在按钮锚点的跨度内。然而,什么都没有改变!

jsFiddle

4

2 回答 2

1

I don't know what your whole code looks like, but you should be able to modify this to do what you are trying to:

$('#id').on('click', function() {
    var that = this;
    $.ajax({
        url: 'wherever.php',
        success: function(html) {
          $(that).find("li span.mi-val").html(html);
        }
    });
于 2013-05-07T21:28:06.783 回答
0

It looks like you have two problems. The first is that as @Barmar posted above, mi-val is a child of micclickks, not a parent. The second is that your ajax complete function is asynchronous, so $(this) isn't what you expect it will be. Both of these are solvable though.

Your code is a bit messy and your html is missing the proper attributes, but this is I think roughly what you want:

$('.kssico').on('click', function(event) {

     var for_uid = $(this).parents("li").attr('data'); //the li element in your html doesn't have a data attribute
     var for_name = $(this).parents("li").attr('unme'); //the li element in your html doesn't have a 'unme' attribute
     var dataString = "for_uid=" + for_uid + "&for_name=" + for_name; 

$.ajax({
    type: "POST",
    context: this,
    url: "include/ajax.php",
    data: dataString,
    success: function (html) {
        if(html=="300")
        {
            $('#myModal .modal-body p').html("Error Please Try Again.");
            $('#myModal').modal('show');
         }
        else
        {
            $(this).parents("li span.mi-val").html(html);
            $('#myModal .modal-body p').html("The Requested Action Has Been Notified To Your Friend.");
            $('#myModal').modal('show');
        }
    }
    });
});
于 2013-05-07T21:40:24.597 回答