私のHTMLはここにあります。私はページに動的に追加されるので、このような要素がたくさんあります。jQuery + Select2各選択値を取得できません
<div class="select2-container select_filter" id="s2id_p_data_1_student">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">
<span class="select2-chosen" id="select2-chosen-3">John Doe</span><abbr class="select2-search-choice-close"></abbr>
<span class="select2-arrow" role="presentation"><b role="presentation"></b></span></a><label for="s2id_autogen3" class="select2-offscreen"></label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-3" id="s2id_autogen3" tabindex="-1">
</div>
<select id="p_data_1_student" name="p_data[p_table_data][1][student]" tabindex="-1" class="select_filter" title="" style="display: none;">
<option></option>
<option value="2460" disabled="">John Doe</option>
<option value="2418" disabled="">Paul Kotula</option>
<option value="2414" disabled="">Andrew Skochelias</option>
</select>
選択オプションを選択した後、選択した値をすべて取得して、他の選択肢で無効にする必要があります。したがって、1つのオプションは1回だけ選択できます。
これを処理するためのjQuery関数があります。しかし、何らかの理由でそれが唯一の最後の選択オプションの値に
function filterSelect() {
jQuery('select.select_filter').each(function() {
var selected = jQuery(this).select2('val');
console.log(selected);
jQuery('.select_filter option').each(function() {
jQuery('.select_filter option[value="' + selected + '"]').prop('disabled', true);
});
});
}
あなたが同じ値を持つ2つの選択があり、いずれかを選択した後、あなたが他の人の値を無効にしたいですか? – daremachine
はい。しかし、必要に応じてページ上に動的に追加するので、選択肢がさらに増えることがあります。しかし、彼らの選択肢は同じです。 –