Click table rows to select checkbox using jQuery

前端 未结 5 1620
难免孤独
难免孤独 2020-12-01 03:07

As I didn\'t find any question asked before, on how to toggle checkbox on click of a table row, so I would like to share my approach to this...

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-01 03:35

    Triggering a click like many of the solutions provided above will cause the function to run twice. Update the prop value instead:

    $('tr').click(function(event){
      alert('function runs twice');
      if(event.target.type !== 'checkbox'){
        //$(':checkbox', this).trigger('click');
        // Change property instead
        $(':checkbox', this).prop('checked', true);
      }
    });
    

    Link to jsfiddle example here

提交回复
热议问题