我如何从我的 HTML 表单中 return 用户输入,以便我可以将 return 传递给另一个函数?
How do i return user input from my HTML form in a way that i can pass the return to another function?
抱歉,如果这已经涵盖了...我正在尝试从基本文本输入表单传递信息,然后使用另外几个函数解析该信息。我无法弄清楚如何 return 来自 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
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>
抱歉,如果这已经涵盖了...我正在尝试从基本文本输入表单传递信息,然后使用另外几个函数解析该信息。我无法弄清楚如何 return 来自 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
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>