use*_*112 5 jquery multi-select select2
我需要捕捉这两个事件:
Reason is - that I want to disable another dropdown (which is responsible for the content of my multiselect) when the first option is selected or all previous selected are deselected.
更新:
$('.projectSelector').on('change', function() {
var targetProject_id = $('#project-id :selected').val();
updateParticpantDropdown(targetProject_id);
});
function updateParticpantDropdown(selectedProjectId){
$.ajax({
type: "POST",
url: '/xx/projects/xx/'+ selectedProjectId,
dataType : "json",
success: function (response, status) {
if(status === "success") {
//console.log('success post');
if(response.result == "error") {
alert(response.message);
}else if (response.result == "success"){
var data = response.data;
$(".participantSelector").empty().select2({
placeholder: "Click here to select participants for above selected project",
allowClear: false,
data: data
});
}
} else if(status === "error") {
// Ajax Post call failed
console.log('fatal ajax post call failed');
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的ajax部分。当我从下拉菜单“.projectSelector”中选择时,我会更新我的多选“.participantSelector”。
到目前为止工作正常!
我不想要的是捕获“.participantSelector”中的第一个选择以禁用“.projectSelector”。反之亦然,如果在 '.participantSelector' 中未选择任何内容,则设置 '.projectSelector' 处于活动状态。
我的 html 看起来像这样:
<select name="participant_id[]" multiple="multiple" class="form-control select2me participantSelector" required="required" id="participant-id"><option value=""></option></select>
Run Code Online (Sandbox Code Playgroud)
从我试过的文档中:
$('select').on('select2:select', function (evt) {
// Do something
});
Run Code Online (Sandbox Code Playgroud)
但这确实会触发下拉列表中的选择 - 但不是我的多选形式的选择。
顺便说一句,我在我的多选中选择了这个错误显示:
类型错误:b.dataAdapter 为空
您可以使用first()方法获取列表中的第一项。请参阅文档,您可以使用:selected选择器获取选定的选项。请参阅文档。
尝试类似:
$('#select').on('change', function() {
var first = $(this).find('option').first().val();
var none = $(this).find('option:selected').length;
if ($(this).val() == first) {
alert('First item selected!');
} else if (none == 0) {
alert('All items deselected!');
}
});
Run Code Online (Sandbox Code Playgroud)