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