简体   繁体   English

基于另一个文本框值将JavaScript输出到文本框

[英]Javascript output to textbox based on another textbox value

I am trying to create a javascript form where there is an input which outputs words into a textbox, then based on the presence of some of the words (not exact phrases) within that box there is a second output in another textbox. 我正在尝试创建一个javascript表单,其中有一个将单词输出到文本框中的输入,然后基于该框中存在的某些单词(不是完全短语),在另一个文本框中有第二个输出。 I have a feeling it probably requires a .search command and something to do with regexp but I am struggling to work out how to implement it. 我觉得它可能需要.search命令以及与regexp有关的内容,但我正在努力研究如何实现它。

The below is not the final implementation, but an example form of the same idea: 以下不是最终的实现,而是具有相同想法的示例形式:

<html>
<head>
<script type="text/javascript">

function calculate(){

var input = document.input.value;
var output1 = document.output1.value;

if (input = 1){
output1 = "x type 1";
}

else if (input = 2){
output1 = "y type 1";
}

else if (input = 3){
output1 = "y type 2";
}

if (output1.search("type 1"){
document.output2.value = "z";
}

}
</script>
</head>
<body>
<form>
Input<input type="textbox" name="input"><br>
<input type="button" onclick="calculate()" value="Calculate" /><br>
<textarea rows="2" name="output1" ></textarea>
<textarea rows="2" name="output2" ></textarea>
</form>
</body>
</html>

So I'm trying to get the function to search the textbox "output1" and if it contains the words "type 1" in this example, then it outputs something into textbox "output2". 因此,我尝试获取该函数以搜索文本框“ output1”,如果在此示例中它包含单词“ type 1”,则它将向文本框“ output2”中输出内容。 I don't think the output1 as a result of the input is working in this example (not sure why) but I'm mainly trying to figure out output2 based on words contained in box output1. 我认为在此示例中,由于输入而导致的output1不能正常工作(不确定为什么),但是我主要是基于基于output1框中包含的单词来找出output2。

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks. 谢谢。

I guess what you're looking for is String.indexOf() . 我猜您正在寻找的是String.indexOf() Example: 例:

var input = 'here i have some words';
var out;

if (input.indexOf('some') !== -1) {
    out = 'I have some in input!';
}

your code has several error mark those error following: 您的代码有几个错误标记以下错误:

  1. document.input.value instead of document.getElementById("input").value document.input.value而不是document.getElementById("input").value
  2. var output1 = document.output1.value; instead of var output1; 代替var output1;
  3. in all if method change input = 1 instead of input == 1 如果方法更改input = 1而不是input == 1
  4. change document.output2.value instead of document.getElementsByName("output2").value 更改document.output2.value而不是document.getElementsByName("output2").value
  5. add document.getElementsByName("output1").value=output1; 添加document.getElementsByName("output1").value=output1; for set value 设定值
  6. add id in html element html元素中添加id

i used to get element from id so added in html element if you use other way learn javaScript 我曾经从id中获取元素,所以如果您使用其他方法学习javaScript,则会在html元素中添加

i think you should need to learn JavaScript well. 我认为您应该很好地学习JavaScript

reformatting your code you wriiten systex lots of error i fixed and following full code: 重新格式化您的代码,您使systex纠错了很多,我已修复并遵循完整的代码:

 function calculate(){ var input = document.getElementById("input").value; var output1; if (input == 1){ output1 = "x type 1"; } else if (input == 2){ output1 = "y type 1"; } else if (input == 3){ output1 = "y type 2"; } if (output1.search("type 1")){ document.getElementById("output2").value="z"; } document.getElementById("output1").value=output1; } 
 <form> Input<input type="textbox" name="input" id="input"><br> <input type="button" onclick="calculate()" value="Calculate" /><br> <textarea rows="2" name="output1" id="output1"></textarea> <textarea rows="2" name="output2" id="output2"></textarea> </form> 

let me know if it's help or any question. 让我知道是否有帮助或有任何疑问。

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

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