以下を使用して、選択したボックスをループできます。
$('#sel_driver_file_options :selected').each(function(i, selected) {
//console.log($(this).val());
var driverName = $(this).val();
var driverText = $(this).text();
$("#tr_file_options_input").show();
$("#span_" + driverName).show();
$('<div id="desc_file_options_input_' + driverName + '">Enter a Value For ' + driverText + '</div>').prependTo($("#span_" + driverName));
}); // end selected each
しかし、選択されていない「#sel_driver_file_options」内のすべてのチェックボックスに対してアクションを実行するにはどうすればよいですか?
私がやろうとしているのは、追加されたものを削除することです:
$('<div id="desc_file_options_input_' + driverName + '">Enter a Value For ' + driverText + '</div>').prependTo($("#span_" + driverName));
ユーザーが選択を解除したとき。それ以外の場合は、もう一度選択すると、2 つ目が作成されます。
編集:私が取り組んでいるコードの完全なブロックの JSFiddle
Edit2: JSFiddle を更新し、整理しやすくするためにテーブルを書き直し、close 関数を次のように変更しました。
close: function (event, ui) {
$('#sel_driver_file_options :selected').each(function (i, selected) {
var driverName = $(this).val();
var driverText = $(this).text();
var driverSpan = $("#span_" + driverName);
var driverDesc = $("#desc_" + driverName);
console.log("Selected = " + driverName);
$("#tr_file_options_input").show();
$(driverSpan).show();
$(driverDesc).show();
}); // end each selected
$('#sel_driver_file_options').not(':selected').each(function () {
//if ($('#sel_driver_file_options').not(':selected')) {
var driverName = $(this).val();
var driverText = $(this).text();
var driverSpan = $("#span_" + driverName);
var driverDesc = $("#desc_" + driverName);
console.log("Not Selected = " + driverName);
$(driverSpan).hide();
$(driverDesc).hide();
// driverSpan.find('[id^="desc_file_options_input_"]').remove();
}); // end each not selected
//$('#desc_file_options_input_' + driverName).remove();
} //end close function
コンソールに次のように表示されます。
Selected = default-priority
Not Selected = default-priority
not(':selected') が機能していないかのように?