jquery不更改下拉元素类吗?
这是下拉列表。我只想选择所选的选项id。提前谢谢jquery不更改下拉元素类吗?,jquery,Jquery,这是下拉列表。我只想选择所选的选项id。提前谢谢 <select id="addbtype" name="addbtype" style="margin-bottom:0px;width:130px;"> <option class='type_selected1' id='qtype_1'>restaurant</option> <option id='qtype_2'>cloth
<select id="addbtype" name="addbtype" style="margin-bottom:0px;width:130px;">
<option class='type_selected1' id='qtype_1'>restaurant</option>
<option id='qtype_2'>clothings</option>
<option id='qtype_8'>paints</option>
</select>
<script>
$('#addbtype option').click(function(){
if(!$(this).hasClass('type_selected1'))
{
var active_element = document.getElementsByClassName('type_selected1');
$(active_element).removeClass('type_selected1');
$(this).addClass('type_selected1');
}
});
var qtid = $('.type_secelcted').attr('id').split('_')[1];
</script>
餐厅
衣服
油漆
$(“#添加类型选项”)。单击(函数(){
if(!$(this).hasClass('type_selected1'))
{
var active_element=document.getElementsByClassName('type_selected1');
$(活动元素).removeClass('type_selected1');
$(this.addClass('type_selected1');
}
});
变量qtid=$('.type_secelcted').attr('id').split('uu')[1];
您需要使用.change()
下拉列表:
$('#addbtype').change(function(){
if(!$(this).find(':selected').hasClass('type_selected1'))
{
var active_element = document.getElementsByClassName('type_selected1');
$(active_element).removeClass('type_selected1');
$(this).addClass('type_selected1');
}
});
您如何使用该类?如果您仅将其用于样式设置,则不需要它,您可以键入
#addtype option[selected] {
//here your css
}
如果要获取选定选项的值-
$('#addbtype').change(function(){
var selectedValue = $(this).val();
});
对于所选选项的id-
$('#addbtype').change(function(){
var selectedItem = $(this).find(":selected");
alert(selectedItem[0].id);
});
演示:试试看,你可以得到“id”
HTML:
<select id="addbtype" name="addbtype" style="margin-bottom:0px;width:130px;">
<option class='type_selected1' id='qtype_1'>restaurant</option>
<option id='qtype_2'>clothings</option>
<option id='qtype_8'>paints</option>
</select>
<button id="pickid">pickid</button>
餐厅
衣服
油漆
皮基德
这里是演示链接试试这个
$("#addbtype").change(function () {
alert($(this).children(":selected").attr("id"));
});
无论我选择了什么@Milind anantwar,我都会得到相同的id qtype_1我想得到选中的选项idI我没有得到选中的元素id,而是得到了我选择的第一个id。要得到选中的选项,请使用
$(this).val()
我不想要它的id值使用$(this)。children(:selected”).attr(“id”)
实际上,我需要在单击按钮时通过ajax发送所选选项id。你能帮我吗..之前我正在更改类并选择所选选项id。实际上,我需要在单击按钮时通过ajax发送所选选项id。之前我正在更改类并选择所选选项id。
$("#addbtype").change(function () {
alert($(this).children(":selected").attr("id"));
});