Javascript 如何将信息传递给新函数

Javascript 如何将信息传递给新函数,javascript,Javascript,需要知道如何将用户名、标题和格式信息从handleFormSubmit传递到addNewPost function handleFormSubmit(event) { // This next line prevents the reload of the form event.preventDefault(); // Get values of inputs // Pass values to addNewPost() var username = documen

需要知道如何将用户名、标题和格式信息从handleFormSubmit传递到addNewPost

function handleFormSubmit(event) {
   // This next line prevents the reload of the form
   event.preventDefault();
   // Get values of inputs
   // Pass values to addNewPost()
   var username = document.getElementById("formUsername").value;
   var caption = document.getElementById("formCaption").value;
   var formImg = document.getElementById("formImg").value;

function addNewPost(username, caption, imgLocation) {




}

您可以在
handleFormSubmit
函数中调用
addNewPost
函数并传递值

function handleFormSubmit(event) {
   // This next line prevents the reload of the form
   event.preventDefault();
   // Get values of inputs
   // Pass values to addNewPost()
   var username = document.getElementById("formUsername").value;
   var caption = document.getElementById("formCaption").value;
   var formImg = document.getElementById("formImg").value;

   addNewPost(username, caption, formImg ); // call the function here.
}
function addNewPost(username, caption, imgLocation) {


}
您可以在
handleFormSubmit
函数中调用
addNewPost(用户名、标题、格式)
。确保在
handleFormSubmit
函数之外声明了
addNewPost()