2011-03-16 168 views
0

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

的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"> 
+0

我没有看到实际的代码中的错误,其中的工作示例的链接去了哪里? – Valerij 2011-03-16 00:35:46

+0

你不应该使用'.attr('value')' - jQuery拥有'.val()'是有原因的。另外,http://jsbeautifier.org/在发布代码之前格式化代码是一件好事。 – ThiefMaster 2011-03-16 00:38:36

回答

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, 

这是错的,不正确的和丑陋的。 &之后不应有空格。其实,传递一个字符串已经很糟糕了。通过这样的对象:使用data: data

这里的固定版本

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

$(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虽则提交该表格..为您节省了大量的工作打字的所有字段名称...

+1

DANGIT!打败我发布它。无论如何,我必须补充的是,他应该使用$ .serialize()来发送他的表单数据 - http://api.jquery.com/serialize/ - 在大小写的情况下手动输入所有这些输入很麻烦他需要在飞行中改变它们。 – mattsven 2011-03-16 00:49:24

+0

确实。甚至可以使用真棒的[jQuery表单插件](http://jquery.malsup.com/form/)。 – ThiefMaster 2011-03-16 00:50:35

+0

感谢您的评论家伙会尝试的意见..对不起,因为丑陋..我在网上找到了代码,并开始使用jQuery .. – Nikon0266 2011-03-16 01:15:33