如何提醒特定选择元素中的选定选项

How to alert selected options from particular select element

本文关键字:选项 选择 何提醒 元素      更新时间:2023-09-26

我在互联网上找到了这段代码。它只是提醒从元素中选择的选项。

我有多个元素,我需要一个特定元素的信息,比如id为"one"的元素。如何将此限制添加到我的代码中。

$("select").change(function () {
      var str = "";
      $("select option:selected").each(function () {
          str += $(this).text() + " ";
      });
     alert(str);
  })
  .trigger("change");

感谢

如果您只想为ID为"one"的元素运行jQuery,请添加ID选择器:

$("select#one").change(function () {
      var str = "";
      $("select#one option:selected").each(function () {
          str += $(this).text() + " ";
      });
     alert(str);
})
.trigger("change");

您应该更改

$("select option:selected").each(function () {

$(this).find("option:selected").each(function () {