Multiple item selection by pressing ctrl button

2019-08-02 00:57发布

问题:

Currently my function looks like that

http://jsfiddle.net/tt13/5CxPr/13/

as you see, now when I click one by one multiple rows, it will make them selected.

What I want to do is, to select multiple rows by pressing ctrl+left-click and one and only selection with only one - left click (I mean remove previous selected and select last clicked one; just like windows icon selection). How can I modify current code?

回答1:

Try this:

$(".subject").live('click',function(event) {
    event.preventDefault();

    if(event.ctrlKey) {
          $(this).toggleClass('selected');  
    } else {
          $(".subject").removeClass("selected");
          $(this).addClass("selected");           
    }
});