首页 > 解决方案 > 如何将信息传递给新功能

问题描述

需要知道如何将用户名、标题和formImg的信息从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) {




}

标签: javascript

解决方案


您可以在addNewPost函数内部调用handleFormSubmit函数并传递值。

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) {


}

推荐阅读