2012-02-22 108 views
0

我有向名,密码和电子邮件jQuery的POST方法register.phpjQuery的POST方法

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'> 

然而似乎没有什么是当按钮被击中时发生

+0

你如何结合你的onclick事件? – IanW 2012-02-22 18:52:52

+0

用按钮名称保存 – foshoeiyyy 2012-02-22 18:53:38

+1

首先,从一些基本的调试开始。使用类似Firebug的东西,并在代码中放置断点以查看实际发生的事情。 (而不是说没有任何事情发生。)函数是否真的被调用?如果是这样,函数中的变量是什么被设置的?它是否会调用'$ .post()'?当时对象的状态是什么?你需要在这里做一些基本的调试来帮助我们。否则,这段代码可能有太多错误。 – David 2012-02-22 19:30:40

回答

3

由于您调用了postData而没有关联的对象,因此函数this内部的内容与window因此您访问的元素都不是您期望的元素。

请勿使用intrinsic event attributes,使用JavaScript绑定您的处理程序。既然你已经在使用jQuery,你应该使用the methods it provides for that

+0

任何解决方案?谢谢 – foshoeiyyy 2012-02-22 18:54:56

+0

查看更新的答案。 – Quentin 2012-02-22 18:55:32

+0

即时通讯不知道该功能知道在哪里可以找到名称密码电子邮件 – foshoeiyyy 2012-02-22 19:04:43

1

这个语法看起来错位

$.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); 
}); 
+0

事实上,我不知道是否“似乎没有发生”是另一种说法,浏览器正在抛出一个JavaScript错误,OP不知道在哪里寻找那些。 – David 2012-02-22 18:57:30

+1

即使这样,“parent”也不是jQuery对象,所以它不会有关联的next()函数来调用。 (或者说,$(this)是没有意义的,所以parent()可能是一个空的jQuery对象) – Dave 2012-02-22 18:57:57

+0

它仍然不能正常工作 – foshoeiyyy 2012-02-22 18:59:13

0

你有2个选项...

要么通过与对象的onclick:

<button onclick='postData(this)' class='regular' name='save'> 

或附上cl使用jQuery ICK处理 - 首选方法使用jQuery的时候:

$('input[name="save"]').click(function() { 
    // your post code here 
}}; 

会有那么没有必要为onclick='postData(this)'

0

发送POST数据这样你会得到正确的输出

名= “示范”; password ='123456'; email ='[email protected]';

 $.post('register.php',{ 'name': name, 'password': password, 'email': email},function(html){ 

           alert(html);  
    },"html"); 

//register.php

回声$ _POST [ '名称'];

+1

当为$ .post创建数据对象时,没有必要将属性名称放入字符串中,除非该名称不是有效的JavaScript变量名称(如'名称[]') – Dave 2012-02-22 19:09:42

0

如果你想让事情正常工作,可以使用jQuery绑定到按钮点击。 (编辑:另外,我们使用非弃用的元素)

<input type='button' class='regular' name='save' id='btnPost' value='Post' /> 

的Javascript:

$(document).ready(function() { 
    $("#btnPost").on("click", function() { 
     var thisBtn = $(this); 
     var parent = thisBtn.parent(); 
     // Replacing this by serializing the parent form - 
     // not sure what parent.data is going to give without seeing the rest 
     // of the html 
     // name = parent.data('name'); 
     // password = parent.data('password'); 
     // email =parent.data('email'); 
     $.post('register.php', thisBtn.closest("form").serialize(),   
      function(data) { 
       parent.next('#message').html(data); 
      }); 
     }); 
    }); 
}); 
0

你可以试试这个:

$(document).ready(function(){ 
    var $this = $('id_of_button'); 
    $this.click(function(){ 
      //ajax logic here 
      $.ajax({ 
       type: 'POST', 
       url: url, 
       data: data,//data to send to serverside 
       success: success, //function to call on success 
       dataType: dataType//datatype eg.text,json etc. 
      }); 
    }); 
});