簡體   English   中英

如何將參數從一個函數傳遞到下一個函數

[英]How to pass parameters from one function to the next

我在將我的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;

而不是做

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

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

現在,您正在程序的開頭調用process函數,並將其返回值分配為onsubmit處理程序。 最后,您當前的版本與此等效:

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

相反,您要做的是讓onsubmit事件處理程序成為一個調用proccess的函數。

你也可以做...

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

如果您的目標平台不支持bind() ,則可以對其進行填充

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM