简体   繁体   English

我如何以一种可以将返回值传递给另一个函数的方式从我的 HTML 表单返回用户输入?

[英]How do i return user input from my HTML form in a way that i can pass the return to another function?

Apologies if this is already covered... I'm trying to pass information from a basic text input form and then parse that information using another couple functions.抱歉,如果这已经涵盖了......我正在尝试从基本文本输入表单传递信息,然后使用其他几个函数解析该信息。 I'm not able to figure out how to return the information from the function in the onClick property to pass it into another function.我无法弄清楚如何从 onClick 属性中的函数返回信息以将其传递给另一个函数。

 function getBulletAction(element) { return element[0].value; } // why can't i store this in a variable outside of the function??? let valueFrmForm = getBulletAction(document.getElementById("frmForm")); document.getElementById("log").innerHTML = valueFrmForm; //I want to pass the value returned from getBulletAction() to getAction() which will change the user input to lowercase characters. function getAction(OutOfTheFunction){ let action = userInput.toLocaleLowerCase('en-US'); return action; }
 <form name="bulletForm" id="frmForm"> Write the action of your bullet:<br> <input type="text" name="bulletAction" value="" style="width: 30%;"/> <input type="button" name="submitType" value="Submit" onClick="getBulletAction(this.parentElement)" /> </form> <h3>This is where the data is supposed to appear from getBulletAction()</h3> <p id="log"></p>

 function getBulletAction(element) { const val = element[0].value; console.log(val); return val; } 
 <form name="bulletActionForm"> Write the action of your bullet:<br> <input type="text" name="bulletAction" value="" style="width: 30%;" /> <input type="button" name="submitType" value="Submit" onClick="getBulletAction(this.parentElement)" /> </form> 

This will return the value of what is in name=bulletAction as a string :) 这将以字符串形式返回name=bulletAction中的值:)

You are storing value in global variable when script loads(at the time when was empty)您在脚本加载时将值存储在全局变量中(当时为空)

  1. Create one global variable at the start of script在脚本开始时创建一个全局变量
  2. Change value of global variable when user clicks submit button当用户单击提交按钮时更改全局变量的值
  3. Use the global variable in another function在另一个function使用全局变量

 var text = ""; function getBulletAction(form) { text = form[0].value; //first input element return form[0].value; } function myFunc(form){ var inputText = getBulletAction(form); document.getElementById("log").innerHTML = getAction(); } function getAction(){ //let's use global variable let action = text.toLocaleLowerCase('en-US'); return action; } function alertMyGlobalVar(){ alert(text); }
 <form name="bulletForm" id="frmForm"> Write the action of your bullet:<br> <input type="text" name="bulletAction" value="" style="width: 30%;"/> <input type="button" name="submitType" value="Submit" onClick="myFunc(this.parentElement)" /> </form> <button onclick="alertMyGlobalVar()">Current value of global variable</button><br> <h id="log">This is where the data is supposed to appear from getBulletAction()</h3>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM