javascript - 下拉字段中插入的文本未反射(reflect)

标签 javascript html

我有一个下拉菜单,其中有其他选项,当选择其他选项时,会出现一个新字段,我们可以在其中输入指定其他的文本,但是当填充文本时,我无法在该字段中填充数据,我只能得到其他

HTML:

<html>
 <head>
  <script type="text/javascript">
   function showfield(name){
   if(name=='Other')document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
   else document.getElementById('div1').innerHTML='';
    }
  </script>
 </head>
 <body>
  <select name="drop" id="drop" onchange="showfield(this.options[this.selectedIndex].value)">
  <option selected="selected">Please select ...</option>
  <option value="car">car</option>
  <option value="bike">bike</option>
  <option value="Other">Other</option>
  </select>
  <div id="div1"></div>
 </body>
</html>

以及如何放置帮助文本,当我们在选择其他文本框时将数据插入到新文本框中时,该帮助文本将会消失。

提前致谢。

最佳答案

试试这个,它对你有用

 <html>
 <head>
  <script type="text/javascript">
   function showfield(name){
    var name=document.getElementById("drop").value;
    if(name=='Other'){
            document.getElementById('div1').innerHTML='Other: <input type="text" name="other" />';
        }
        else {
                document.getElementById('div1').innerHTML='';
        }
    }
  </script>
 </head>
 <body>
  <select name="drop" id="drop" onchange="showfield()">
  <option selected="selected">Please select ...</option>
  <option value="car">car</option>
  <option value="bike">bike</option>
  <option value="Other">Other</option>
  </select>
  <div id="div1"></div>
 </body>
</html>

关于javascript - 下拉字段中插入的文本未反射(reflect),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35739427/

相关文章:

javascript - 如何在具有类型的 TypeScript 项目中 Vue.use() 一个 javascript 插件

javascript - 将 "Delete"按钮添加到我的计算器

html - DIV 相对于窗口垂直居中。可水平滚动。固定div旁边

加载页面时 JavaScript 未运行`

jquery 动画无法工作

javascript - Async/await - 等待函数和非等待函数的组合

javascript - 每次ajax成功函数循环在codeigniter中完成时尝试将数据添加到新的div

javascript - Fancybox - Youtube 嵌入 100%,同时保持纵横比

css - 围绕中心 Logo 图像进行居中导航

PHP 字段验证 - (2 个字符后跟 6 个数字)示例 : bp125489