如何以可以将返回传递给另一个函数的方式从我的 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>

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

  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>

最新更新