Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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 post后div中不显示任何内容_Javascript_Jquery_Html_Css_Ajax - Fatal编程技术网

Javascript post后div中不显示任何内容

Javascript post后div中不显示任何内容,javascript,jquery,html,css,ajax,Javascript,Jquery,Html,Css,Ajax,此功能用于在#消息div 但是,div中不会显示任何内容 $(document).ready(function() { $("#register button[name=btnPosta]").click(function() { console.log('clicked'); thisBtn = $(this); parent = $(this).parent(); name = parent.data('name');

此功能用于在#消息
div
但是,
div
中不会显示任何内容

$(document).ready(function() {
    $("#register button[name=btnPosta]").click(function() {
        console.log('clicked');

        thisBtn = $(this);
        parent = $(this).parent();
        name = parent.data('name');

        $(this).attr('disabled', true);
        $.post('register.php', {
            name: ('name')
        }, function(data) {
            console.log('Ajax success');
            parent.next('#message').html(data);
            thisBtn.attr('disabled', false); // reset  });

            console.log('Ajax success');
        });
    });
});​
只是对代码的一些建议。这些变量主要是在全局范围内创建的,很可能没有任何原因


只是对代码的一些建议。主要是在全局范围内创建变量,很可能没有任何原因。

假设您的标记与一小时前发布的标记相似:

$(function() {
    $("#register").find("button[name=btnPosta]").click(function() {
        console.log('clicked');

        //notice the use of `var` to keep the variables local, which will be faster
        var thisBtn = $(this),
            parent  = thisBtn.parent(),
            name    = parent.data('name');

        //notice the use of `.prop()` instead of `.attr()`, `.prop()` is new as of jQuery 1.6, so if you are using an older version then `.attr()` is the way to go
        thisBtn.prop('disabled', true);
        $.post('register.php', {
            name: name //notice here that this references the `name` variable set further up
        }, function(data) {
            console.log('Ajax success');

            parent.next('#message').html(data);
            thisBtn.prop('disabled', false); // reset  });

            console.log('Ajax success');
        });
    });
});​


我的建议只是一条红鲱鱼:

定义变量时,请确保使用
var
关键字。如果没有
var
,您将在
Global
对象上创建一个属性(或者在浏览器中运行JavaScript时在
窗口中创建一个属性)。但是,
parent
已经是
window
的属性。在IE中,
window.parent
是只读的,因此永远不会设置您的值,当您调用
parent.data()
时,您将得到一个错误

当我使用
var
时,您的代码对我有效:

假设您的标记与一小时前发布的标记相似:

$(function() {
    $("#register").find("button[name=btnPosta]").click(function() {
        console.log('clicked');

        //notice the use of `var` to keep the variables local, which will be faster
        var thisBtn = $(this),
            parent  = thisBtn.parent(),
            name    = parent.data('name');

        //notice the use of `.prop()` instead of `.attr()`, `.prop()` is new as of jQuery 1.6, so if you are using an older version then `.attr()` is the way to go
        thisBtn.prop('disabled', true);
        $.post('register.php', {
            name: name //notice here that this references the `name` variable set further up
        }, function(data) {
            console.log('Ajax success');

            parent.next('#message').html(data);
            thisBtn.prop('disabled', false); // reset  });

            console.log('Ajax success');
        });
    });
});​


我的建议只是一条红鲱鱼:

定义变量时,请确保使用
var
关键字。如果没有
var
,您将在
Global
对象上创建一个属性(或者在浏览器中运行JavaScript时在
窗口中创建一个属性)。但是,
parent
已经是
window
的属性。在IE中,
window.parent
是只读的,因此永远不会设置您的值,当您调用
parent.data()
时,您将得到一个错误

当我使用
var
时,您的代码对我有效:

尝试使用firebug等开发工具进行调试,以查看您得到的响应?您看到控制台消息了吗?您看到控制台上的“Ajax成功”了吗?从外观上看,您试图使用具有相同
消息
ID的多个元素,这可能会导致奇怪的错误,这完全是错误的。如果是这种情况,请将其更改为类,并将选择器更改为
.message
。ID应该是唯一的,如果只有一个
#message
元素,那么
$('#message').html(数据)应该可以做到这一点。可能重复使用firebug等开发工具进行调试,以查看您得到的响应?您看到控制台消息了吗?您看到控制台上的“Ajax成功”了吗?从外观上看,您尝试使用具有相同
消息
ID的多个元素,这可能会导致奇怪的错误,这完全是错误的。如果是这种情况,请将其更改为类,并将选择器更改为
.message
。ID应该是唯一的,如果只有一个
#message
元素,那么
$('#message').html(数据)
应该可以做到这一点。的可能重复项
$('#message').html(data);