i've got a weird behavior with jquery and checkboxes. In my form i build a table with some data. user can click on row or on input checkbox for select a row and add them to the selection. I build this fiddle for example So when you click on row it's ok, the row is selected and i've got my id. But when you first click on the input, so the input go checked, but when we release the button he got unchecked. After if we click again, the behavior is correct. I know i've done something wrong, but can't find out what. I need to use mousedown event for multiple selection, and allow user to select multiple id only with one click.
_isMouseDown = false;
jQuery(document).on('mousedown',function(event) {
_isMouseDown = true;
});
jQuery(document).on('mouseup',function(event) {
_isMouseDown = false;
});
(function($) {
var sTableId = 'lois_server_select';
var aAvailableServerClicked = new Array();
var aServerSelectedClicked = new Array();
$('.lois_server_select tr').hover(
function () {
if(_isMouseDown == true){
//exec onmousedown
$(this).trigger('mousedown');
}
},function (){}
);
//bug with the first click on checkbox unchecke the input
$('.lois_server_select input').click(function(evt){
_that = $(this);
if(_that.closest('tr').hasClass('clicked')){
_that.prop('checked',true);
} else {
_that.prop('checked',false);
}
});
$('.lois_server_select tr').bind('mousedown',function(evt) {
console.log('this',this);
var _that = $(this);
var _checkbox = $(':checkbox',_that);
console.log('checkbox',_checkbox);
console.log('start checked:',_checkbox.attr('checked'));
if(_checkbox.val() == undefined) return;
console.log('checked:',_checkbox.prop('checked'));
if(_that.hasClass('clicked')){
//remove class
_that.removeClass('clicked');
//uncheck checkbox
_checkbox.prop('checked',false);
//remove to aAvailableServerClicked
if(sTableId == 'lois_server_select'){
aAvailableServerClicked.splice($.inArray(_checkbox.val(), aAvailableServerClicked),1);
} else {
aServerSelectedClicked.splice($.inArray(_checkbox.val(), aServerSelectedClicked),1);
}
} else {
//add selected class
_that.addClass('clicked');
//check checkbox
_checkbox.prop('checked',true);
//add to aAvailableServerClicked
if(sTableId == 'lois_server_select'){
aAvailableServerClicked.push(_checkbox.val());
} else {
aServerSelectedClicked.push(_checkbox.val());
}
}
console.log('end checked:',_checkbox.attr('checked'));
//evt.stopPropagation();
});
})(jQuery);
Thx for your help.