jQuery表单提交问题

时间:2011-03-16 00:11:21

标签: jquery forms

我有一个表单,我试图在fancybox(www.fancybox.net)内提交。如果我直接进入表格,它将提交罚款。但是,如果我在页面内部加载表单我想从表单中调用fancybox将不会提交并且fancybox消失并且页面刷新。我已经尝试了同样的问题fancybox和facebox ..下面是我在主页面中的所有Jquery代码。此页面打开fancybox并随后打开提交表单。该表单应该在提交时显示来自窗口的感谢信息而不是刷新页面..非常感谢任何帮助:

JavaScript的:

$(document).ready(function () {

    $("#various1").fancybox({
        'opacity': true,
        'overlayShow': false,
        'transitionIn': 'none',
        'autoscale': 'false',
        'transitionOut': 'none'
    });

    $("submit").submit(function () {
        alert('it submits');

        // we want to store the values from the form input box, then send via ajax below
        var name = $('#name').attr('value');
        var email = $('#email').attr('value');
        var password = $('#password').attr('value');
        var custom1 = $('#custom1').attr('value');
        var custom2 = $('#custom2').attr('value');
        var custom3 = $('#custom3').attr('value');
        var custom4 = $('#custom4').attr('value');
        var custom5 = $('#custom5').attr('value');
        var custom6 = $('#custom6').attr('value');
        var custom7 = $('#custom7').attr('value');
        var custom8 = $('#custom8').attr('value');
        var custom9 = $('#custom9').attr('value');
        var custom10 = $('#custom10').attr('value');
        $.ajax({
            type: "POST",
            url: "test.php",
            data: "name=" + name + "& email=" + email + "& password=" + password + "& custom1=" + custom1 + "& custom2=" + custom2 + "& custom3=" + custom3 + "& custom4=" + custom4 + "& custom5=" + custom5 + "& custom6=" + custom6 + "& custom7=" + custom7 + "& custom8=" + custom8 + "& custom9=" + custom9 + "& custom10=" + custom10,

            success: function () {

                $('submit').fadeOut(function () {
                    $('div.success').fadeIn();
                });
            }
        });
        return false;
    })

});

链接到表单:

<a id="various1" href="register.php">

1 个答案:

答案 0 :(得分:0)

data: "name=" + name + "& email=" + email + "& password=" + password + "& custom1=" + custom1 + "& custom2=" + custom2 + "& custom3=" + custom3 + "& custom4=" + custom4 + "& custom5=" + custom5 + "& custom6=" + custom6 + "& custom7=" + custom7 + "& custom8=" + custom8 + "& custom9=" + custom9 + "& custom10=" + custom10,

这是错误的,不正确的和丑陋的。 &之后不应该有空格。实际上,传递一个字符串已经很糟糕了。传递这样的对象:

var data = { name: name, email: email, password: password, custom1: custom1, ... };

使用data: data

这是固定版本:

$(document).ready(function () {

    $("#various1").fancybox({
        'opacity': true,
        'overlayShow': false,
        'transitionIn': 'none',
        'autoscale': 'false',
        'transitionOut': 'none'
    });

    $("form").submit(function () {
        var data = {
            name: $('#name').val(),
            email: $('#email').val(),
            password: $('#password').val(),
            custom1: $('#custom1').val(),
            custom2: $('#custom2').val(),
            custom3: $('#custom3').val(),
            custom4: $('#custom4').val(),
            custom5: $('#custom5').val(),
            custom6: $('#custom6').val(),
            custom7: $('#custom7').val(),
            custom8: $('#custom8').val(),
            custom9: $('#custom9').val(),
            custom10: $('#custom10').val()
        };

        $.ajax({
            type: "POST",
            url: "test.php",
            data: data,
            success: function() {
                $('submit').fadeOut(function () {
                    $('div.success').fadeIn();
                });
            }
        });
        return false;
    })

你应该真的使用jquery form plugin来提交那个表格。为你节省很多输入所有字段名称的工作......