javascript - 通过选择的选项更改 div 的值

标签 javascript html

我必须编写一些代码,其中选择中有三个选项,并且 div 的值在更改时将更改为所选项目的值,我尝试了很多方法和函数,但没有任何效果。这是我当前的代码:

$('select[class="country"]').on('change', function () {
    var get = $('select option:selected').text();
    $('#fDiv').text = get;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>The coolest country</h1>
<select name="country"id="country" class="country">
  <option value="Korea">Korea</option>
  <option value="China">China</option>
  <option value="Romania">Romania</option>
</select>
<div id="fDiv"></div>

请分享有关此代码可能存在的问题的任何想法。

最佳答案

使用.text(),例如:$('#fDiv').text(myText);

$('select[class="country"]').on('change', function() {
  var myText = $('select option:selected').text();
  $('#fDiv').text(myText);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>The coolest country</h1>
<select name="country" id="country" class="country">
  <option value="Korea">Korea</option>
  <option value="China">China</option>
  <option value="Romania">Romania</option>

</select>
<div id="fDiv"></div>

关于javascript - 通过选择的选项更改 div 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59060007/

相关文章:

html - 字体在 IE7 和 8 中看起来更大 - 但只有一个网站的一页

javascript - 在 CKEditor 中仅添加一次 HTML 元素

javascript - Ionic Angular js隐藏的输入值不显示在另一个页面中

javascript - 如何制作不可见的倒计时,在页面关闭后继续?

javascript - Google 图表中的分页?

javascript - 我使用 document.getelementsbyclassname 是否错误?

javascript - 尝试从本地主机获取 json 文件的数据时如何解决 CORS Origin 问题

javascript - 查找网站中所有已加载的文件以创建 ApplicationCache list

php - 使用 href 到两个链接(php 和网页)

jquery - 弹出文本编辑器时内容可编辑的光标位置在可编辑的div中