Ajax表单提交

时间:2012-07-12 21:55:11

标签: php jquery ajax post

嘿伙计们我没有收到任何帖子数据,而且非常积极,这根本不起作用。最重要的是它使用id#附加了一个接受/拒绝,以及该表单复选框的值。我没有错误,也没有任何警告所以有一个问题踩到它=(希望另一组眼睛?

很抱歉,我知道我的JQuery会受到打击。

    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load("jquery", "1.6");
    </script>

    <script>
        $(".audit").submit(function() 
            { 
                return false; 
            });

        $(".accept, .deny").click(function(event) 
            {
                $form = $(this).parent("form").get(0);
                $gruden = $form(function(index) { attributes.push($(this).val());});
                $.post($form.attr("action"), $form.serialize() + "&submit=" + $(this).attr("value") + "&gruden=" + $gruden, function(data) {

                console.log(data);

            });
        });
    </script>

.......................

<?php foreach($obj->photos as $pending) { ?>

        <div class='container' id='photo-<?=$pending->id;?>'>

            <span class='shadow'>
            <a href='/<?=$pending->large;?>'><img src='http://<?=$_SERVER['HTTP_HOST'].'/'.$pending->small;?>'/></a>
            </span>

            <form class='audit' name='audit-<?=$pending->id;?>' action='<?=$_SERVER['PHP_SELF'];?>'>
            <div class='box'>

                <ul>

                    <li>ID:   <?=$pending->id;?></li>
                    <li>Date: <?=$pending->created_at;?></li>
                    <li>User: <?=$pending->fb_id;?></li>
                    <li>&nbsp;</li>

                    <li>

                        <input class='gruden' value='gruden-<?=$pending->id;?>' type='checkbox' />
                        <a name='submit' value='accept-<?=$pending->id;?>' class='accept' href=''></a>
                        <a name='submit' value='deny-<?=$pending->id;?>' class='deny' href=''></a>

                    </li>

                </ul>

            </div>
            </form>

        </div>

<?php } ?>

2 个答案:

答案 0 :(得分:0)

此行肯定不会返回您的表单元素,因为它只查看直接父级。

$form = $(this).parent("form");

尝试这样的事情:

$form = $(this).parents("form").get(0);

答案 1 :(得分:0)

我想我现在看到了你的问题。我重写了你的代码,所以它更有意义。评论解释发生了什么

<script>
    $(".audit").submit(function(){ 
            return false; //Preventing the form from submitting if someone hits Enter
    });

    $(".accept, .deny").click(function(event) {
            var form = $("form.audit"); //Always use var to declare variables in Javascript. It's not PHP; we don't use $, unless you want it to be a part of the variable name. Also fixed your selector
            var gruden = $form(function(index) { attributes.push($(this).val());}); //Are you trying to find out if the checkbox with class gruden is checked? Please answer in the comments so I can correct this line
            $.post(form.attr("action") + "&submit=" + $(this).attr("value") + "&gruden=" + gruden, form.serialize(), function(data) { 
                   console.log(data);
            }); //I assume that you want to pass submit and gruden as GET params here, since you're appending them to the URL. If you want them to go as POST vars, then let me know in the comments
    });
</script>

没有冒犯,但我想你可能会从阅读更多关于Javascript和jQuery的内容中受益。至少要了解选择器如何在jQuery中工作,以及一些基本的Javascript语法