我对如何使用jQuery.valid插件访问提交的表单感到困惑。
在 API 选项的"成功"选项中:http://jquery.malsup.com/form/#options-object
它说成功函数中的第 4 个参数是 jquery 包装的表单对象,但我尝试使用 jQuery 访问它时一直说它undefined
。
以下是成功函数在其示例页面上的外观:http://jquery.malsup.com/form/#ajaxSubmit
function showResponse(responseText, statusText, xhr, $form) {
var id=$form.attr('id');
console.log('id:'+id);
}
不幸的是,控制台.log说Uncaught TypeError: Cannot call method 'attr' of undefined.
有什么想法吗?
谢谢提姆
变量不能以$
开头。删除$
,然后重试?
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);
}
希望这有帮助! :)
我通过每次都显式写出 jQuery 表单选择器而不是尝试将其作为对象传递来解决这个问题。
因此,我没有试图传递$form
而是使用了这个:
$('#myForm').attr(...)
它更冗长,但至少它有效!