#jquery #html #jquery-ui #jquery-plugins
#jquery #HTML #jquery-ui #jquery-плагины
Вопрос:
У меня есть выпадающий список, подобный этому
<select multiple="multiple">
<optgroup label='name1'>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</optgroup>
<optgroup label='name2'>
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</optgroup>
Я использую плагин jQuery UI MultiSelect Widget для нескольких выпадающих списков, но я должен выбрать только один вариант из каждого параметра optgroup.
Например. Если для ‘name1’ выбран параметр «First», то «second», «third» не следует выбирать для параметров optgroup ‘name1’.
Комментарии:
1. Создайте скрипку
2. можете ли вы создать этот код в fiddle.
Ответ №1:
Попробуйте это
var memoryOne;
var memoryTwo;
$("option").mouseover(function () {
var selectedOne = $("optgroup:nth-of-type(1)").children("option:selected").index();
var selectedTwo = $("optgroup:nth-of-type(2)").children("option:selected").index();
memoryOne = selectedOne;
memoryTwo = selectedTwo;
}).click(function () {
var theSelectedIndex = $(this).index();
var theParentIndex = $(this).parent().index();
setTimeout(function () {
clickEvent(theSelectedIndex, theParentIndex, memoryOne, memoryTwo);
}, 1);
});
function clickEvent(passedIndex, parentIndex, memoryOne, memoryTwo) {
var selectionOne = memoryOne;
var selectionTwo = memoryTwo;
var theParent = $("optgroup:eq(" parentIndex ")");
var theOption = $("optgroup:eq(" parentIndex ") option:eq(" passedIndex ")");
var theGrandParent = $("select");
theParent.find("option:not(option:eq(" passedIndex "))").prop("selected", false);
if (parentIndex == 0) {
$("optgroup:not(optgroup:eq(" parentIndex "))").children("option:eq(" selectionTwo ")").prop("selected", true);
} else if (parentIndex == 1) {
$("optgroup:not(optgroup:eq(" parentIndex "))").children("option:eq(" selectionOne ")").prop("selected", true);
}
}