Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在ajax中得到这个结果_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 在ajax中得到这个结果

Javascript 在ajax中得到这个结果,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图制作一个脚本,将p行更改为输入字段,让用户编辑它们,然后在通过ajax签入外部php文档后恢复为p行。然而问题似乎是我不能在ajax部分中使用它,它破坏了代码。我怎样才能解决这个问题?我需要发布HTML吗 $(document).ready(function () { function changeshit(result, that) { if (result == "success") { $(that).closest('div').

我试图制作一个脚本,将p行更改为输入字段,让用户编辑它们,然后在通过ajax签入外部php文档后恢复为p行。然而问题似乎是我不能在ajax部分中使用它,它破坏了代码。我怎样才能解决这个问题?我需要发布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);


                }
            });
        }

    });
});
$(文档).ready(函数(){
函数更改(结果,即){
如果(结果=“成功”){
$(that).最近的('div').find('input').each(函数)(){
var el_naam=$(即.attr(“名称”);
var el_id=$(that.attr(“id”);
var el_content=$(即.attr(“值”);
以“

”+el_content+“

”)替换; }); $(“.editlink”)。替换为(“”); }否则{ 警报(结果); } } $(.editinv”)。在('click','a',函数(){ var editid=$(this.attr(“id”); var edit_或_text=$(this.attr(“name”); 如果(编辑或文本==“编辑”){ $(this).最近的('div')。查找('p')。每个(函数(){ var el_naam=$(this.attr(“名称”); var el_id=$(this.attr(“id”); var el_content=$(this).text(); $(此)。替换为(“”); }); $(“.editlink”)。替换为(“”); }否则如果(编辑或文本==“完成”){ var poststring=“”; $(this).最近的('div').find('input').each(函数)(){ var el_naam=$(this.attr(“名称”); var el_id=$(this.attr(“id”); var el_content=$(此).attr(“值”); poststring=poststring+''+elu-naam+'='+elu-content+'&'; }); poststring=poststring+'end=end' $.ajax({ url:'http://'+document.domain+'/klanten/updateaddress.php', 键入:“post”, 数据:poststring, 成功:功能(结果,此){ 改变(结果,这个); } }); } }); });
尝试以下操作:

在('click','a',函数(){add

$(".editinv").on('click', 'a', function () {
    var element = this;
然后将此更改为:

$.ajax({
    url: 'http://' + document.domain + '/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    success: function (result) {
        changeshit(result, element);
    }
});
也就是说,如果我正确理解了您要做的事情,请尝试以下操作:

在('click','a',函数(){add

$(".editinv").on('click', 'a', function () {
    var element = this;
然后将此更改为:

$.ajax({
    url: 'http://' + document.domain + '/klanten/updateaddress.php',
    type: 'post',
    data: poststring,
    success: function (result) {
        changeshit(result, element);
    }
});
也就是说,如果我正确理解了您试图做的事情,只要添加以下内容:

context: this
对于
$.ajax
选项,则将使用
this
的正确值自动调用
success
处理程序,因此您不需要
this
参数

然后,您也不再需要
success
回调周围的额外函数包装器,因此您只需使用:

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

context: this
对于
$.ajax
选项,则将使用
this
的正确值自动调用
success
处理程序,因此您不需要
this
参数

然后,您也不再需要
success
回调周围的额外函数包装器,因此您只需使用:

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

是的,常见的解决方案是声明一个var示例self=this并使用该变量

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

            changeshit(result, self);


            }
        });
    }

通过这种方式,此上下文保存在变量中。

是的,常见的解决方案是声明一个var示例self=this并使用该变量

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

            changeshit(result, self);


            }
        });
    }

这样,此上下文将保存在变量中。

有几种方法可以实现此目的

1) 如果阅读文档(),您将看到可以为ajax方法提供上下文

上下文 类型:PlainObject此对象将成为所有Ajax相关回调的上下文 表示调用中使用的ajax设置($.ajaxSettings) 将设置传递到$.ajax)

通过这种方式,你甚至可以像下面的代码一样使用它

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) 您可以利用闭包(或搜索google),这样您的代码就可以

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);
    }
});


作为旁注,我建议您使用变量/对象缓存,因此在函数顶部声明
var$this=$(this)
,并在函数中使用它,而不是每次需要时调用
$(this)

<

1) 如果阅读文档(),您将看到可以为ajax方法提供上下文

上下文 类型:PlainObject此对象将成为所有Ajax相关回调的上下文 表示调用中使用的ajax设置($.ajaxSettings) 将设置传递到$.ajax)

通过这种方式,你甚至可以像下面的代码一样使用它

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) 您可以利用闭包(或搜索google),这样您的代码就可以

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);
    }
});


作为旁注,我建议您使用变量/对象缓存,因此在函数顶部声明
var$this=$(this)
,并在函数中使用它,而不是调用
$(this)
每次你都需要它。

changeshit
需要两个参数,你只提供了一个。忘了在我粘贴的代码中更改它,它在那里不起作用。添加它似乎破坏了整个代码。如果没有它,它只能正确地找到结果。通过AJAX响应中的
这个
你到底想得到什么?Since
this
不是函数中的局部变量,它没有保存在闭包中,因此不会持久化到回调。这就是为什么像
var=this;
这样的东西在Javascript中是一个常见的习惯用法。@ZathrusWriter看起来希望单击的元素
changeshit
需要两个参数,您只提供一个。forg若要在粘贴的代码中更改,则不会