我有一个jquery post方法,它将姓名、密码和电子邮件发送到register.php
function postData()
{
thisBtn = $(this);
parent = $(this).parent();
name = parent.data('name');
password = parent.data('password');
email =parent.data('email');
$.post('register.php', {name: (name), password: (password), email: (email)}, function(data) ;
parent.next('#message').html(data);
}执行onclick功能的按钮:
<button onclick = 'postData()' class='regular' name='save'>但是,当按下按钮时,似乎没有任何反应
发布于 2012-02-23 02:54:14
由于您在没有关联对象的情况下调用postData,因此函数内的this与window相同,因此您访问的元素都不是您期望的元素。
不要使用intrinsic event attributes,使用JavaScript绑定您的处理程序。既然您已经在使用jQuery,那么您应该使用the methods it provides for that。
发布于 2012-02-23 02:55:28
此语法看起来已损坏
$.post('register.php', {name: (name), password: (password), email: (email)}, function(data) ;
parent.next('#message').html(data);
// no {} for function, no closing ) for $post, and premature ;试一试
$.post('register.php', {name: (name), password: (password), email: (email)}, function(data) {
parent.next('#message').html(data);
});发布于 2012-02-23 02:57:06
你有两个选择...
在onclick中传递和对象:
<button onclick='postData(this)' class='regular' name='save'>或者在使用jQuery时使用jQuery首选方法附加单击处理程序:
$('input[name="save"]').click(function() {
// your post code here
}};这样就不需要onclick='postData(this)'了。
https://stackoverflow.com/questions/9401110
复制相似问题