javascript - 通过上面的输入更改选择/选项通用文本

标签 javascript forms

我的目标是让文本字段中的文本输入替换选择/选项下拉列表中的通用选项。我现在所做的不起作用,并显示为未定义。 这是我的标记:(请包含一个普通的 javascript 选项) HTML:

<input id="name1" type="text" placeholder="name here" onKeyUp="return change1(this)">
<input id="name2" type="text" placeholder="name here" onKeyUp="return change2(this)">

等等

<h4>Choose person:</h4>
<select id="select1">
<option id="pers1" value="1">person</option>
<option id="pers2" value="2">person</option>

..还有其中几个

Javascript:

function change1(input){
 document.getElementById("pers1").text =
 input.text; }
function change2(input){
 document.getElementById("pers2").text =
 input.text; }

等等

最佳答案

<script>
    function change1(input){
        document.getElementById("pers1").text = input.value; 
    }
    function change2(input){
        document.getElementById("pers2").text = input.value; 
    } 
</script>
<h4>Choose person:</h4>
<select id="select1">
    <option id="pers1" value="1">person</option>
    <option id="pers2" value="2">person</option>
</select>
<hr>
<input id="name1" type="text" placeholder="name here" onKeyUp="change1(this)">
<input id="name2" type="text" placeholder="name here" onKeyUp="change2(this)">

关于javascript - 通过上面的输入更改选择/选项通用文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20931296/

相关文章:

javascript - 检测从父项(开启者)关闭的新选项卡

javascript - 如果字段为空 - 做一件事情,如果不做其他事情(jQuery vs 浏览器字段自动完成)

ruby-on-rails - rails : Simpleform inside of table input type = text_area- how to set its height?

asp.net - 使用 javascript 动态创建表单有什么注意事项吗?

php - 表单 Action 问题

java - 在 Spring 的 Controller 中存储状态

javascript - 动态 Moment.js 差异

javascript - AngularJS ng-click stopPropagation

javascript - 在javascript上获取未定义的 "Uncaught TypeError: Cannot read property ' top'

php - JQuery AJAX 发布并刷新页面内容,无需重新加载页面