简体   繁体   English

html根据输入字段更改文本

[英]html change text based on input field

I have an <h3> that I want the value of to change based on what is typed in the input field. 我有一个<h3> ,我想根据输入字段中输入的内容来更改的值。 Here is the code I currently have: 这是我目前拥有的代码:

<input id="Medication" name="Medication" size="50" type="text" value="">
<script>
$(document).ready(function () {
  $('#Medication').change(function () {
    $('#myAnchor').text($(this).val());
  });
});
</script>
<h3 id="myAnchor"></h3>

There are many different ways to accomplish this: 有许多不同的方法可以完成此操作:

Solution #1 using jQuery 使用jQuery的解决方案#1

$(document).ready(function () {
 $('#Medication').keyup(function () {
 $('#myAnchor').text($(this).val());
});
});

Solution #2 using Javascript - onkeyup 使用Javascript的解决方案#2-onkeyup

 function myFunction(input){ var elementValue = input.value; document.getElementById("myAnchor").innerHTML = elementValue; } 
 <input id="Medication" name="Medication" onkeyup = myFunction(this) size="50" type="text" value=""> <h3 id="myAnchor"></h3> 

Solution #3 using Javascript - addEventListener 使用Javascript的解决方案#3-addEventListener

 document.getElementById("Medication").addEventListener("keyup", myFunction); function myFunction() { var elementValue = document.getElementById("Medication").value; document.getElementById("myAnchor").innerHTML = elementValue; } 
 <input id="Medication" name="Medication" size="50" type="text" value=""> <h3 id="myAnchor"></h3> 

Hope it helps! 希望能帮助到你!

use a keyup handler to dynamically change the text 使用keyup处理程序动态更改文本

$(document).ready(function () {
  $('#Medication').keyup(function () {
    $('#myAnchor').text($(this).val());
  });
});

You can use keyup or keydown 您可以使用keyupkeydown

<input id="Medication" name="Medication" size="50" type="text" value="">
<script>
$(document).ready(function () {
  $('#Medication').keyup(function () {
    $('#myAnchor').text($(this).val());
  });
});
</script>
<h3 id="myAnchor"></h3>

The jquery change or javascript onchange event is fired when you leave the input field and click somewhere else. 当您离开输入字段并单击其他位置时,将触发jquery change或javascript onchange事件。

The keyup or keydown functions are fired every time you press or release a key. 每次按下或释放一个键时,都会触发键上或键下功能。 If you want simultaneous writing in the h3 element, keyup or keydown are the best options. 如果要同时写入h3元素,则keyup或keydown是最佳选择。

It works and is economical if you do it inline: 如果您以内联方式进行操作,那么它既有效又经济:

<input type="text" onchange="$('#myAnchor').text(this.value)">

or 要么

<input type="text" onkeydown="$('#myAnchor').text(this.value)">

though, as commented below, if your script is going to get a lot more complex, it's better not to do things (javascript, css, etc) inline. 但是,如下文所述,如果您的脚本要复杂得多,最好不要内联处理(javascript,css等)。

Is it possible to have multiple functions to change multiple ids separately? 是否可以使用多种功能分别更改多个ID? for example: 例如:

Javascript : Javascript:

function myFunction(input){
    var elementValue = input.value;
    document.getElementById("myAnchor","myAnchor2","myAnchor3").innerHTML = 
    elementValue;
}

HTML : HTML:

<input id="Medication" name="Medication" onkeyup = myFunction(this) size="50" type="text" value="">

<input id="Medication" name="Medication" onkeyup = myFunction(this) size="50" type="text" value="">

<input id="Medication" name="Medication" onkeyup = myFunction(this) size="50" type="text" value="">


<div id="myAnchor"></div>
<div id="myAnchor2"></div>
<div id="myAnchor3"></div>

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

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