如何将参数从一个函数传递到下一个函数

时间:2014-09-26 01:03:32

标签: javascript parameters

我无法将我的id参数传递给我的流程函数。当我加载页面时,由于某种原因,它自动运行,而不必单击提交按钮,然后如果我尝试在我的表单上添加新信息,它不会做任何事情。我真的想在提交时从init函数传递参数。

function process(first, last, dept) {
    'use strict';


     return false;
} // End of process() function.

// Initial setup:
function init() {
    'use strict';
    $('theForm').onsubmit = process('firstName', 'lastName', 'department');
} // End of init() function.
window.onload = init;

2 个答案:

答案 0 :(得分:2)

而不是做

$('theForm').onsubmit = process('firstName', 'lastName', 'department');

DO

$('theForm').onsubmit = function(){
    process('firstName', 'lastName', 'department');
}

现在,您在程序开始时调用process函数并将其返回值指定为onsubmit处理程序。最后,您当前的版本等同于:

process('firstName', 'lastName', 'department');
$('theForm').onsubmit = false;

相反,你想要做的是让onsubmit事件处理程序成为调用proccess的函数。

答案 1 :(得分:0)

你也可以......

$('theForm')
     .onsubmit = process.bind(window, 'firstName', 'lastName', 'department');

如果您的目标平台不支持bind(),您可以polyfill it