jQuery 获取选择选项的名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18697843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 22:15:21  来源:igfitidea点击:

jQuery get the name of a select option

jqueryhideattr

提问by Sam Skirrow

I have a dropdown list with several option, each option has a name attribute. When I select an option, a different list of checkboxes needs to appear - when another options is selected, that checkbox list should disappear and another one be displayed.

我有一个包含多个选项的下拉列表,每个选项都有一个 name 属性。When I select an option, a different list of checkboxes needs to appear - when another options is selected, that checkbox list should disappear and another one be displayed.

I have created these checkbox lists and given them an ID that correlates to the name attribute of the option selected. I am trying to use the following code to display the correct checkbox list

我创建了这些复选框列表,并为它们提供了一个与所选选项的名称属性相关的 ID。我正在尝试使用以下代码来显示正确的复选框列表

$(document).ready(function(){

       $('#band_type_choices').on('change', function() {         
    $('.checkboxlist').hide();
    $('#checkboxlist_' + $(this).attr("name") ).css("display", "block");

});

However nothing is happening.

然而,什么也没有发生。

Here is my dropdown options:

这是我的下拉选项:

<select id="band_type_choices">
    <option vlaue="0"></option>
    <option value="100" name="acoustic">Acoustic</option>
    <option value="0" name="jazz">Jazz/Easy Listening</option>
    <option value="0" name="acoustic_jazz">Acoustic + Jazz/Easy Listening</option>
    <option value="0" name="party">Party</option>
    <option value="0" name="acoustic_party">Acoustic + Party</option>
    <option value="0" name="jazz_party">Jazz/Easy Listening + Party</option>
    <option value="0" name="acoustic_jazz_party">Acoustic + Jazz/Easy Listening + Party</option>
 </select>

and an example of one of the lists:

以及其中一个列表的示例:

<div class="checkboxlist" id="checkboxlist_acoustic" style="display:none;">
<input type="checkbox" class="checkbox keys" name="keys" value="100" />Keys<br>
<input type="checkbox" class="checkbox acou_guit" name="acou_guit" value="100" />Acoustic Guitar<br>
<input type="checkbox" class="checkbox drums" name="drums" value="100" />Drums<br>
<input type="checkbox" class="checkbox alt_sax" name="alt_sax" value="100" />Alto Sax<br>
<input type="checkbox" class="checkbox ten_sax" name="ten_sax" value="100" />Tenor Sax<br>
<input type="checkbox" class="checkbox clarinet" name="clarinet" value="100" />Clarinet<br>
<input type="checkbox" class="checkbox trombone" name="trombone" value="100" />Trombone<br>
<input type="checkbox" class="checkbox trumpet" name="trumpet" value="100" />Trumpet<br>
<input type="checkbox" class="checkbox flute" name="flute" value="100" />Flute<br>
<input type="checkbox" class="checkbox cello" name="cello" value="100" />Cello<br>
<input type="checkbox" class="checkbox violin" name="violin" value="100" />Violin<br>
</div>

回答by jgerman

For anyone who comes across this late, like me.

对于像我这样遇到这么晚的人。

As others have stated, nameisn't a valid attribute of an optionelement. Combining the accepted answer above with the answer from this other question, you get:

正如其他人所说,name不是option元素的有效属性。将上面接受的答案与其他问题答案相结合,您会得到:

$(this).find('option:selected').text();

回答by Mohammad Adil

In your codethisrefers to the select element not to the selected option

在您的代码中this是指选择元素而不是选定的选项

to refer the selected option you can do this -

要参考所选选项,您可以执行此操作 -

$(this).find('option:selected').attr("name");

回答by Rory McCrossan

Firstly nameisn't a valid attribute of an optionelement. Instead you could use a dataparameter, like this:

首先name不是option元素的有效属性。相反,您可以使用一个data参数,如下所示:

<option value="foo" data-name="bar">Foo Bar</option>

The main issue you have is that the JS is looking at the nameattribute of the selectelement, not the chosen option. Try this:

您遇到的主要问题是 JS 正在查看元素的name属性select,而不是选择的选项。尝试这个:

$('#band_type_choices').on('change', function() {         
    $('.checkboxlist').hide();
    $('#checkboxlist_' + $('option:selected', this).data("name")).css("display", "block");
});

Note the option:selectedselector within the context of the selectwhich raised the change event.

请注意引发更改事件option:selected的上下文中的选择器select

回答by Manu M

 $(this).attr("name") 

means the name of the select tag not option name.

表示选择标签的名称而不是选项名称。

To get option name

获取选项名称

 $("#band_type_choices option:selected").attr('name');

回答by Gowtham Ag

The Code is very Simple, Lets Put This Code

代码很简单,放上这段代码

var name = $("#band_type_choices  option:selected").text();

Here You don't want to use $(this).find().text(), directly you can put your id name and add option:selectedalong with text().

这里你不想用的$(this).find().text(),直接把你的id名加上就可以 option:selectedtext()

This will return the result option name. Better Try this...

这将返回结果选项名称。最好试试这个...

回答by mplungjan

Using name on a select option is not valid.

在选择选项上使用名称无效。

Other have suggested the data- attribute, an alternative is a lookup table

其他人建议使用 data- 属性,另一种方法是查找表

Here the "this" refers to the select so no need to "find" the option

这里的“this”是指选择所以不需要“find”选项

var names = ["", "acoustic", "jazz", "acoustic_jazz", "party", "acoustic_party", "jazz_party", "acoustic_jazz_party"];

$(function() {
  $('#band_type_choices').on('change', function() {
    $('.checkboxlist').hide();
    var idx = this.selectedIndex;
    if (idx > 0) $('#checkboxlist_' + names[idx]).show();
  });
});
.checkboxlist { display:none }
Choose acoustic to see the corresponding div

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="band_type_choices">
  <option vlaue="0"></option>
  <option value="100" name="acoustic">Acoustic</option>
  <option value="0" name="jazz">Jazz/Easy Listening</option>
  <option value="0" name="acoustic_jazz">Acoustic + Jazz/Easy Listening</option>
  <option value="0" name="party">Party</option>
  <option value="0" name="acoustic_party">Acoustic + Party</option>
  <option value="0" name="jazz_party">Jazz/Easy Listening + Party</option>
  <option value="0" name="acoustic_jazz_party">Acoustic + Jazz/Easy Listening + Party</option>
</select>
<div class="checkboxlist" id="checkboxlist_acoustic">
  <input type="checkbox" class="checkbox keys" name="keys" value="100" />Keys<br>
  <input type="checkbox" class="checkbox acou_guit" name="acou_guit" value="100" />Acoustic Guitar<br>
  <input type="checkbox" class="checkbox drums" name="drums" value="100" />Drums<br>
  <input type="checkbox" class="checkbox alt_sax" name="alt_sax" value="100" />Alto Sax<br>
  <input type="checkbox" class="checkbox ten_sax" name="ten_sax" value="100" />Tenor Sax<br>
  <input type="checkbox" class="checkbox clarinet" name="clarinet" value="100" />Clarinet<br>
  <input type="checkbox" class="checkbox trombone" name="trombone" value="100" />Trombone<br>
  <input type="checkbox" class="checkbox trumpet" name="trumpet" value="100" />Trumpet<br>
  <input type="checkbox" class="checkbox flute" name="flute" value="100" />Flute<br>
  <input type="checkbox" class="checkbox cello" name="cello" value="100" />Cello<br>
  <input type="checkbox" class="checkbox violin" name="violin" value="100" />Violin<br>
</div>