【发布时间】:2016-11-08 22:04:16
【问题描述】:
我在弄清楚如何将变量传递给ajaxForm() 函数的参数时遇到了问题。单击 jQuery 中的表单按钮后,我从隐藏的表单字段获取 templatedir 路径(这很好用),我可以将它传递给其他函数,但不能传递给 ajaxForm() 参数:
var templatedir;
// Get variable of templatedir
$('form#contact input[type="submit"]').on('click',function(){
templatedir = $('input[name=templatedir]').fieldValue();
});
// Validate Form
function validateform(formData, jqForm, options) {
...
// IF I CALL TEMPLATEDIR HERE IT WORKS
}
// Send to PHPMailer
$("form#contact").ajaxForm({
url: templatedir, // IT DOESNT WORK HERE
type: 'post',
beforeSubmit: validateform,
target: '#msg',
clearForm: true
});
如您所见,我尝试了此答案 (How do i pass variables between functions in javascript) 中的解决方案,但它不起作用。
【问题讨论】:
-
嗯...是这样吗?
templatedir = $('input[name=templatedir]').fieldValue(); -
是的,这是正确的,我可以将结果传递给console.log,它工作得很好。