2012-06-08 30 views
1

我很困惑如何使用jQuery.validate插件访问提交的表单。jquery.validate插件访问ajax成功回调中的表单

在的API选项“成功”选项:http://jquery.malsup.com/form/#options-object

它说,在成功函数的第四个参数是jQuery的包装形式的对象,但我试图用jQuery访问它一直说其undefined

这里的成功,函数的样子他们的榜样页:http://jquery.malsup.com/form/#ajaxSubmit

function showResponse(responseText, statusText, xhr, $form) { 
    var id=$form.attr('id'); 
    console.log('id:'+id); 
} 

不幸的是,执行console.log说:Uncaught TypeError: Cannot call method 'attr' of undefined.

有什么想法?

感谢, 添

+0

我想你使用的jQuery <1.4,因为它在'()'中有一些注释,请参阅 – thecodeparadox

+0

@thecodeparadox,谢谢我使用jQuery1.7.1。我看到那张纸条,但不知道我是否误解了一些东西。 –

+0

@timpeterson它解决了吗? –

回答

0

我解决了这个问题,每次都明确地写出了jQuery表单选择器,而不是将它作为对象传递。

因此,而不是试图绕过$form我用这个:

$('#myForm').attr(...) 

更冗长,但至少它的工程!

0

变量不能与$我想开始。删除$并再试一次?

function showResponse(responseText, statusText, xhr, form) { 
    var id = form.attr('id'); 
    console.log('id:'+id); 
} 

或者这可能是另一种解决办法,如果不是一个jQuery object

function showResponse(responseText, statusText, xhr, form) { 
    var id = $(form).attr('id'); 
    console.log('id:'+id); 
} 

其他可能性

function showResponse(responseText, statusText, xhr, form) { 
    var id = $(form).attr('id'); 
    console.log('id:'+id); 
} 
function showResponse(responseText, statusText, xhr, form) { 
    var id = form.attr('id'); 
    console.log('id:'+id); 
} 
function showResponse(responseText, statusText, xhr, $form) { // Won't work 
    var id = form.attr('id'); 
    console.log('id:'+id); 
} 
function showResponse(responseText, statusText, xhr, $form) { // High chance 
    var id = $($form).attr('id'); 
    console.log('id:'+id); 
} 

希望这有助于! :)

+0

谢谢,但没有运气,仍然'undefined' –

+0

可以有更多的可能性,'$()'包装和只是前置'$'。让我们知道是否有问题解决。 :) –

+0

你可以把这些可能性在你的答案?我不确定你的意思 –