没有发生在jQuery post [英] Nothing happening on jQuery post

查看:178
本文介绍了没有发生在jQuery post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的jQuery函数,用于将名称,密码和电子邮件的值发布到 register.php ,然后显示一条消息在

Below is my jQuery function that is used to post the values of name, password and email to register.php and then display a message echoed in register.php indiv#message.

但是当我点击

jQuery

$(document).ready(function() {
    $("#btnPost").on("click", function() {
        console.log('clicked'); 
        var thisBtn = $(this); 
        var parent = thisBtn.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);
            }
        );
    });
});

这是HTML块,其中所有的数据都输入,但是没有什么似乎发生,当按钮 #postBtn 被点击。

This is the HTML block where all the data is input, however nothing seems to be happening when the button #postBtn is clicked.

<table>
    <tr>
        <td> Username: <td>
        <td> <input type = "text"  class = "field" name = "name"> </td>
    </tr>
    <tr>
        <td> Password: <td>
        <td> <input type = "password" class = "field" name = "password"> </td>
    </tr>
        <td > Email: <td>
        <td> <input type = "email" class = "field" name = "email"> </td>
    </tr>
    <tr>
        <td>  <td>
        <td> <button  class='regular' id = "btnPost" name='save'> Log In  </button></td>
    </tr>
    <tr>
</table>


推荐答案

您的问题是您使用 .next(),它返回当前元素的下一个兄弟节点。因为你传递一个选择器,你只会得到一个结果,如果下一个兄弟满足选择器,否则你什么也没有。只需使用 $(#message),因为 #message 不是下一个兄弟元素。

Your problem is your use of .next(), which returns the next sibling of the current element. And since you are passing in a selector, you will only get a result if the next sibling satisfies the selector, otherwise you get nothing. Just use $("#message") since #message is not the next sibling following the parent element of your button.

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

这篇关于没有发生在jQuery post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆