简体   繁体   English

使用jQuery从一个选择框(选择的optgroup和选项)中获取2个值

[英]Get 2 values from ONE selectbox (optgroup and option selected) with jQuery

Can somebody help me with this, I have a selectbox that looks like this: 有人可以帮我吗,我有一个如下所示的选择框:

<select name="scatid">
<optgroup label="June">
  <option>2005</option>
  <option>2006</option>
  <option>2007</option>
  <option>2008</option>
</optgroup>
<optgroup label="July">
  <option>2005</option>
  <option>2006</option>
</optgroup>

and I also have an empty inputbox... 而且我还有一个空的输入框...

What I'm trying to achieve is, if for example I select from the selectbox year 2008 (June optgroup)... the inputbox value get's automaticaly the text "June 2008" (optgroup name + option selected)... etc etc.. 我要实现的目标是,例如,如果我从选择框年份2008(6月optgroup)中选择...,则输入框的值会自动获得文本“ 2008年6月”(选择的optgroup名称+选项)...等等。 。

Can somebody give me an example how can I achieve this? 有人可以给我一个例子,我该如何实现呢? Thank you very much 非常感谢你

Just use value attributes to give the value you want. 只需使用值属性即可提供所需的值。

<select name="scatid">
<optgroup label="June">
  <option value="June 2005">2005</option>

附带条件,您实际上并不想这样做:

$("#scatid").find("option:selected").parent().attr('label')

You can get the label by reading label property of the optgroup element: 您可以通过阅读optgroup元素的label属性来获取标签:

$('select[name="scatid"]').on('change', function(){
   var label = this.options[this.selectedIndex].parentNode.label;
   $('input[type=text]').val(label + ' ' + this.value);
});

http://jsfiddle.net/uEFx3/ http://jsfiddle.net/uEFx3/

Try this using JQuery: 使用JQuery尝试以下操作:

$(function(){
$('select[name="scatid"]').change(function(){
    alert($(this).find('option:selected').parent('optgroup').attr('label')+'  '+$(this).val());
});
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取从SelectBox Angularjs中选择的值 - Get values selected from SelectBox Angularjs jQuery从selectbox中获取选定值不起作用 - jQuery get selected value from selectbox not working 如何使用jQuery从multipal selectbox值中获取所有选定的选项 - How to get all the selected option from multipal selectbox value using jQuery JQuery - 从Selectbox中删除除first和selected之外的所有选项 - JQuery - Remove all option from Selectbox except for first and selected 当用户在jQuery中选择一个选项时,如何获取选择框的值和价格? - How to get selectbox values and prices when user select a option in jQuery? 保存/存储来自optgroup的选定选项的名称 - Saving/Storing the name of a selected option from an optgroup 选择optgroup中的选项之一时如何禁用未选择的选项 - How to disable unselected option when one of option in optgroup is selected 使用jquery获取optgroup中select选项的索引 - Get index of select option in an optgroup with jquery Bootstrap multiselect 获取 optgroup 和 option 的值 - Bootstrap multiselect to get values of optgroup and option 如何使用jQuery将选定项从一个下拉组列表(optgroup)移动到另一个下拉组列表(optgroup)? - How to move selected items from one dropdown group list (optgroup)to another dropdown group list (optgroup) using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM