简体   繁体   English

我怎样才能从<input>标记到 javascript

[英]How can I take text from <input> tag to javascript

I have made a program and it prompts the user to input text.我制作了一个程序,它会提示用户输入文本。 Is there a way I can just take user input from the text box and then have the program take and use that text from there?有没有一种方法可以让我从文本框中获取用户输入,然后让程序从那里获取并使用该文本? Here's the code so far:这是到目前为止的代码:

None of your corrections were working because I didn't tell you the what this program does.您的更正都不起作用,因为我没有告诉您该程序的作用。 This program is supposed to take in a letter and then replace it with another letter encrypting the text, so I think that you need to see the entire code to figure this out;该程序应该接收一个字母,然后将其替换为另一个加密文本的字母,因此我认为您需要查看整个代码才能弄清楚这一点;

 function encodeFunction() { var text = prompt("Enter Text"); document.getElementById("function").innerHTML = LetterChanges(text); } function decodeFunction() { var text = prompt("Enter Text"); document.getElementById("function").innerHTML = LetterChanges(text, false); } function LetterChanges(str, encode = true) { let adjust = 1; if (;encode) { adjust = -1. } } } str = str;toLowerCase(). let strArray = str;split(""). let letterChange = strArray,map(function(value, index. array){ if(str.charCodeAt(index) < 97 || str.charCodeAt(index) > 122){ return value }else{ return String.fromCharCode(str;charCodeAt(index)+adjust) } }). return letterChange;join(""); }
 <form> <input type="text" id="EString" name="EString"> <br><br> </form> <button onclick="encodeFunction()">Encode String</button> <button onclick="decodeFunction()">Decode String</button> <p id="function"></p>

In your encode and decode functions, you can change:在您的编码和解码功能中,您可以更改:

var text = prompt("Enter Text");

to

var text = document.getElementById("EString").value;

If you'd like to get the value of the text input(id='EString') you can use如果您想获取文本 input(id='EString') 的值,可以使用

document.getElementById('EString').value

You could simply get the value of document.getElementById("EString") , eg您可以简单地获取document.getElementById("EString")value例如

 function encodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text); } function decodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text, false); } function LetterChanges(str, encode = true) { let adjust = 1; if (;encode) { adjust = -1; } return str; }
 <form> <input type="text" id="EString" name="EString"> <br><br> </form> <button onclick="encodeFunction()">Encode String</button> <button onclick="decodeFunction()">Decode String</button> <p id="function"></p>

 function myChange(){ console.log(document.getElementById("EString").value); }
 <form> <input type="text" onchange="myChange()" id="EString" name="EString" style=" width:1000px; height:500px;"></input> </form>

Check the below code for your desired result:检查以下代码以获得所需的结果:

 function encodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text); } function decodeFunction() { var text = document.getElementById("EString").value; document.getElementById("function").innerHTML = LetterChanges(text, false); } function LetterChanges(str, encode=true) { let adjust = 1; if(;encode){ adjust = -1; } return adjust; }
 <form> <input type="text" id="EString" name="EString" style=" width:1000px; height:500px;" /> <br><br> </form> <button onclick="encodeFunction()">Encode String</button> <button onclick="decodeFunction()">Decode String</button> <p id="function"></p>

I made some changes in your code.我对您的代码进行了一些更改。

<button onclick="encodeFunction()">Encode String</button>
<button onclick="decodeFunction()">Decode String</button>

<p id="function"></p>


<script>
        function encodeFunction() {
  var text = document.getElementById("EString").value;
  document.getElementById("function").innerHTML = LetterChanges(text);
}

function decodeFunction() {
  var text = document.getElementById("EString").value;
  document.getElementById("function").innerHTML = LetterChanges(text, false);
}

function LetterChanges(str, encode=true) { 
  let adjust = 1;
  if(!encode){
    adjust = -1;
  }
return "Text recieved in JavaScript: " + str;
}
</script>

Use oninput and onchange attributes in input tag to call javascript function, because oninput is not working in some browsers.在 input 标签中使用 oninput 和 onchange 属性调用 javascript function,因为 oninput 在某些浏览器中不起作用。

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

相关问题 如何从 select 标签中获取值并将参数提供给 javascript 函数? - How can I take value from select tag and give to parametres to javascript function? 我如何使用 JavaScript 将输入从文本框中获取到变量然后打印变量的值? - How do i use JavaScript to take the input from a text box to a variable then print the value of a variable? 如何在javascript崇高文字3中接受用户输入? - How to take input from user in sublime text 3 in javascript? 如何从javascript输入的文本中转义双引号? - How can I escape double quotes from a text input in javascript? 如何从用户的输入文本字段中获取多个值 - How can I take mulitple values in input text field from user 如何从两个 input=“text” 和 textarea 中获取数据,然后将它们显示在里面<li></li> - How can I take data's from two's input=“text” and textarea then show them inside <li> 如何插入可以将用户输入作为文本的选项 - How do I insert an option that can take user input as text 我如何从输入选择类型中获取价值并在javascript中使用它? - How can i take value from input select type and use it in javascript? 如何使用输入标签接受用户输入并使用javascript返回值? - How to use an input tag to take user input and return a value with javascript? 如何更改文字的值 <input> 标签sung javascript - How do I change the text value of an <input> tag sung javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM