得到这个在ajax结果

im trying to make a script that changes P lines to input fields, let user edit them and then revert back to p lines after a check in an external php document through ajax. However the problem seems to be that I cant use this within the ajax part, it breaks the code. How can I solve that? Do I need to post the HTML?

$(document).ready(function () {

    function changeshit(result, that) {
        if (result == "success") {

            $(that).closest('div').find('input').each(function () {


                var el_naam = $(that).attr("name");
                var el_id = $(that).attr("id");
                var el_content = $(that).attr("value");

                $(that).replaceWith("<p name='" + el_naam + "' id='" + el_id + "'>" + el_content + "</p>");

            });


            $(".editlink").replaceWith("<a href=\"#_\" class=\"editlink\" name=\"edit\" id=\"" + editid + "\">Bewerken</a>");
        } else {

            alert(result);
        }
    }



    $(".editinv").on('click', 'a', function () {

        var editid = $(this).attr("id");
        var edit_or_text = $(this).attr("name");

        if (edit_or_text == "edit") {


            $(this).closest('div').find('p').each(function () {

                var el_naam = $(this).attr("name");
                var el_id = $(this).attr("id");
                var el_content = $(this).text();

                $(this).replaceWith("<input type='text' name='" + el_naam + "' id='" + el_id + "' value='" + el_content + "' />");

            });


            $(".editlink").replaceWith("<a href=\"#_\" class=\"editlink\" name=\"done\" id=\"" + editid + "\">Klaar</a>");

        } else if (edit_or_text == "done") {


            var poststring = "";
            $(this).closest('div').find('input').each(function () {

                var el_naam = $(this).attr("name");
                var el_id = $(this).attr("id");
                var el_content = $(this).attr("value");

                poststring = poststring + '' + el_naam + '=' + el_content + '&';

            });

            poststring = poststring + 'end=end'

            $.ajax({
                url: 'http://' + document.domain + '/klanten/updateaddress.php',
                type: 'post',
                data: poststring,
                success: function (result, this) {

                    changeshit(result, this);


                }
            });
        }

    });
});

Try the following:

Right under $(".editinv").on('click', 'a', function () { add

$(".editinv").on('click', 'a', function () {
    var element = this;

And then change this to:

$.ajax({
    url: 'http://' + document.domain + '/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    success: function (result) {
        changeshit(result, element);
    }
});

That is if I am understanding correctly what you are trying to do

If you simply add:

context: this

to the $.ajax options then the success handler will automatically be called with the correct value of this, so you won't need the that parameter.

You'll then also no longer need the extra function wrapper around the success callback, so you can just use:

$.ajax({
    url: 'http://' + document.domain + '/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    context: this,       // propagate "this"
    success: changeshit  // just pass the func ref
});

Yes, the common solutions is declare a var example self = this and use that variable

 var self = this;
 $.ajax({
            url: 'http://'+document.domain+'/klanten/updateaddress.php',
            type: 'post',
            data: poststring,
            success: function(result) {   

            changeshit(result, self);


            }
        });
    }

In that way, the this context is save in the variable.

There are a few ways you can achieve this

1) If you read the docs (jQuery.ajax) you'll see that you can supply a context to the ajax method

context Type: PlainObject This object will be made the context of all Ajax-related callbacks. By default, the context is an object that represents the ajax settings used in the call ($.ajaxSettings merged with the settings passed to $.ajax).

$.ajax({
    url: 'http://'+document.domain+'/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    context: this,
    success: function(result) {   
        // the context sent above would become the context of this function when called by jquery
        changeshit(result, this);
    }
});

Using it this way you could even do it like the bellow code

function changeshit (result) {
    var $that = $(this);
    if (result == "success") {

            $that.closest('div')... // cool ha ?
};
$.ajax({
    url: 'http://'+document.domain+'/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    context: this,
    success: changeshit
});

2) You can take advantage of closures ( read more here or search google ), so your code would become

var context = this;
$.ajax({
    url: 'http://'+document.domain+'/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    success: function(result) {   
        // here you can use any variable you declared before the call
        changeshit(result, context);
    }
});

As a side note, i would recommend you use variable/object caching, so declare var $this = $(this) at the top of the function and use it thruought your function, instead of calling $(this) each time you need it.