[英]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);
});
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.