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

很抱歉,如果已经解决了...我正在尝试从基本文本输入表单传递信息,然后使用其他几个函数解析该信息。我无法弄清楚如何从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>

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

在脚本加载时(在为空时)您正在将值存储在全局变量中

  1. 在脚本开头创建一个全局变量
  2. 更改全局变量的值当用户单击“提交”按钮时
  3. 在另一个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>

本文链接:https://www.f2er.com/3039082.html

大家都在问