首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jquery无法在单击时选择和取消选择td

Jquery无法在单击时选择和取消选择td
EN

Stack Overflow用户
提问于 2012-08-16 05:10:36
回答 1查看 559关注 0票数 0

我已经写了2个jquery函数。第1-11行是第一行,它在mousedown上绑定mouseover事件,以获得单击并拖动以选择td的效果(内部选中/取消选中复选框)。

第二个功能(12-20)是点击和取消点击单个td以选中和取消选中复选框。我可以按下鼠标并选择多个td,但不能在单个td上单击和取消单击。我不确定问题出在哪里?任何建议都是值得感谢的。

代码如下:

代码语言:javascript
复制
$("#tbl td").mousedown(function() {
    $("#tbl td").bind('mouseover', function() {
        var checkbox = $(':checkbox', this)[0];
        $(this).css({
            'background': (checkbox.checked ? 'white' : '#6D7B8D')
        });
        checkbox.checked = !checkbox.checked;
    });
    $(this).mouseover();
}).mouseup(function(event) {
    $("#td").unbind('mouseover');
    event.preventDefault();
    event.stopPropagation();
});

$('#tbl td').click(function(e) {
    if ($(this).find('input:checkbox').is(':checked')) {
        $(this).find('input:checkbox').attr("checked", "");
        $(this).css({
            background: "white"
        });
    } else {
        $(this).find('input:checkbox').attr("checked", "checked");
        $(this).css({
            background: "#6D7B8D"
        });
    }
});​

谢谢

EN

回答 1

Stack Overflow用户

发布于 2012-08-16 06:14:44

嗯,我不确定这是否是你真正需要的,但也许能帮上忙。

HTML (只想知道,如果你把代码放进去会很有帮助!)

现场演示: http://jsfiddle.net/oscarj24/TATg7/

代码:

代码语言:javascript
复制
$('#tbl').on('mousedown, mouseover, mouseup, click', 'td', function(e) {
    if(e.type == 'mousedown'){
        $(this).mouseover();
    } else if(e.type == 'mouseover'){
        var checkbox = $('input:checkbox');
        $(this).css({'background': (checkbox.is(':checked') ? 'white' : '#6D7B8D')});
        checkbox.prop('checked', !checkbox.checked);    
    } else if(e.type == 'mouseup'){
        $(this).unbind('mouseover');
        e.preventDefault();
        e.stopPropagation();
    } else if(e.type == 'click'){
        var checked = $('input:checked').is(':checked');
        checked ? background = 'white' : background = '#6D7B8D';
        $('input:checked').prop('checked', !checked);
        $(this).css({background: background});
    }
});
​
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11977364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档