2013-08-03 60 views
0

我能夠遍歷我的選擇框:JQuery的 - 使用做點事同時選中和未選中複選框

$('#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)); 

當用戶取消選擇它。否則,如果他們再次選擇它,則會創建第二個。

編輯:JSFiddle for the full block of code I am working on

EDIT2:更新的jsfiddle,重新寫了表,優化管理,改變了關閉功能,以這樣的:

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')不工作?

+0

你可以創建一個HTML工作撥弄包括 –

+0

中添加了問題的鏈接 –

回答

-1

像這樣的工作

$('#sel_driver_file_options').not(':selected'); 
+0

似乎並沒有工作,要麼:( –

0

我已經把選擇到一個數組,然後再次運行。每次和隱藏陣列中所有跨越不整理出來。 它現在按預期工作。 Here's the JSFiddle for the working solution in case anyone needs it.

而這裏的相關代碼:

$("#sel_driver_file_options").multiselect({ 
    show: ["blind", 200], 
    hide: ["blind", 500], 
    multiple: true, 
    selectedList: 3, 
    noneSelectedText: "Select file() options", 
    open: function (event, ui) { 
     // nothing for now 
    }, 
    close: function (event, ui) { 
     var arr = []; 
     $('#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); 
      arr.push(driverName); 
      console.log("Selected = " + driverName); 
      $("#tr_file_options_input").show(); 
      $(driverSpan).show(); 
      $(driverDesc).show(); 
     }); // end each selected 
     // Now loop through all options and remove spans for unselected ones 
     $('#sel_driver_file_options option').each(function() { 
      var driverName = $(this).val(); 
      var driverText = $(this).text(); 
      var driverSpan = $("#span_" + driverName); 
      var driverDesc = $("#desc_" + driverName); 
      if ($.inArray(driverName, arr) == -1) { 
       console.log("Not in array: " + driverName); 
       $(driverSpan).hide(); 
       $(driverDesc).hide(); 
      } 
     }); // end each not selected 
    } //end close function 
}); // end multiselect 
相關問題