我试图从下拉列表中获取所选选项,并用该文本填充另一个项目,如下所示.IE正在掀起风暴,但它在Firefox中不起作用:

$('#ddlCodes').change(function() {
  $('#txtEntry2').text('#ddlCodes option:selected').text();
});

我做错了什么?

推荐答案

Here's a short version:

$('#ddlCodes').change(function() {
  $('#txtEntry2').text($(this).find(":selected").text());
});

karim79是个不错的 Select ,从您的元素名称判断,txtEntry2可能是一个文本框,如果它是任何类型的输入,您将需要使用.val()来代替,或者使用.text(),如下所示:

  $('#txtEntry2').val($(this).find(":selected").text());

For the "what's wrong?" part of the question: .text() doesn't take a selector, it takes text you want it set to, or nothing to return the text already there. So you need to fetch the text you want, then put it in the .text(string) method on the object you want to set, like I have above.

Jquery相关问答推荐

在 Mastodon 中将 jQuery 添加到 Rails 6

为什么在 jQuery 插件中返回 this.each(function())?

在 JavaScript 中解析 URL

确定 JavaScript 值是否为整数?

如何使用 jQuery UI Resizable 仅水平或垂直调整大小?

使用 Javascript|jQuery 删除特定的内联样式

如何使用 jQuery 进行带命名空间的 XML 解析

数字键盘的keyCode值?

jQuery:value.attr 不是函数

jQuery - 多个 :not Select 器

C# String.IsNullOrEmpty Javascript 等效项

如何使用 jQuery 停止默认链接点击行为

Isotope 和 Masonry jQuery 插件之间的区别

如何删除/更改 JQuery UI 自动完成助手文本?

jquery 检测某些类的 div 已添加到 DOM

如何使用 jQuery 删除 cookie?

5秒后jQuery自动隐藏元素

在 jQuery 中 Select 后代元素的最快方法是什么?

使用 jQuery 在 Select 列表中隐藏选项

小于 10 给数字加 0