【问题标题】:Highlight each row when selecting multiple rows选择多行时突出显示每一行
【发布时间】:2011-05-04 07:06:36
【问题描述】:

我正在使用这个 Jquery 代码一次选择多行。如您所见,我尝试使用代码“lastChecked.style.background = “yellow”;”更改背景颜色但它不起作用。我该怎么做?

var lastChecked = null;

$(document).ready(function() {
        $('.chkbox').click(function(event) {
                if(!lastChecked) {
                        lastChecked = this;
                        return;
                }

                if(event.shiftKey) {
                    var start = $('.chkbox').index(this);
                    var end = $('.chkbox').index(lastChecked);

                    for(i=Math.min(start,end);i<=Math.max(start,end);i++) {
                                $('.chkbox')[i].checked = lastChecked.checked;
                                lastChecked.style.background = "yellow";
                        }
                }

                lastChecked = this;
        });
});

这里是所有使用的代码:

{% extends "base.html" %}
{% block title %}Most Common Calls | CPRS Help{% endblock %}
{% block script %}
<script type="text/javascript">
/*<![CDATA[*/
function highlight_row(row_id,checkbox)
{
  var row = document.getElementById(row_id);
  row.style.background = checkbox.checked ? "yellow" : ""; 
}

function unhighlight_row(row_id)
{
  var row = document.getElementById(row_id);
  row.style.background = "white"; // background yellow
}
/*]]>*/
</script>
{% endblock %}
{% block content %}
<h2>Most Common Calls</h2>
<form action="/mark_as_uninteresting/" method="post">
{% csrf_token %}
<table class="calls">
<tr><th style="width:30px">N</th><th>Word</th><th style="width:150px;"><input class="searchbutton" type="submit" value="Mark as Uninteresting" /></th></tr>
{% for word in word_frequencies %}
<tr id="row_{{ forloop.counter }}"><td>{{ word.n }}</td><td style="padding:0;"><a href="/search/?q={{ word.word }}" style="padding:5px;display:block;color:blue;">{{ word.word }}</a></td><td style="text-align:center"><input type="checkbox" name="checkbox_{{ word.id }}" onclick="highlight_row('row_{{ forloop.counter }}',this)" id="id_chk{{ forloop.counter }}" class="chkbox" /></td></tr>
{% endfor %}
</table>
</form>
<script type="text/javascript" src="/media/js/jquery-1.5.2.min.js"></script>
<script type="text/javascript">
    var lastChecked = null;

    $(document).ready(function() {

            $('.chkbox').click(function(event) {
                    if(!lastChecked) {
                            lastChecked = this;
                            return;
                    }

                    if(event.shiftKey) {
                        var start = $('.chkbox').index(this);
                        var end = $('.chkbox').index(lastChecked);

                        for(i=Math.min(start,end);i<=Math.max(start,end);i++) {
                                    $('.chkbox')[i].checked = lastChecked.checked;
                            }
                    }

                    lastChecked = this;
            });
    });
</script>
{% endblock %}

【问题讨论】:

  • lastChecked = $(this);和 lastChecked.css("background-color","yellow") 或更改类名(更好)
  • 显示一些 html。最好在这里:jsfiddle.net
  • 谢谢。我添加了所有使用的代码。
  • 您能否发布发送到浏览器的 html(查看源代码),因为这是 JavaScript 使用的。另外:您的“查询”标签是指“jquery”吗? (不建议您应该使用 jQuery,但您似乎正在使用它,并且“查询”标签似乎与您的问题无关。)

标签: javascript jquery checkbox


【解决方案1】:


更新的解决方案:

好的,现在您的问题更清楚了,这是正确的解决方案:

如果您希望在使用 shift 键选择时行具有背景颜色,则需要更改此行:

lastChecked.style.background = "yellow";

到 →

$('.chkbox')[i].parentNode.style.backgroundColor='yellow';

                   或

$('.chkbox').eq(i).parents('tr').style.backgroundColor='yellow';


您的版本尝试在复选框上设置背景颜色。这是不可能的。您需要选中复选框的父节点。

我的解决方案中的第一个版本将针对复选框的直接父级。如果您的&lt;tr&gt; 仅深入一级,则可以在您的情况下使用。但是,如果您的 &lt;tr&gt; 可以更深入(即复选框可能位于 &lt;span&gt; 中,然后位于 &lt;tr&gt; 中),您应该使用第二个版本,它会在复选框的祖先中搜索 &lt;tr&gt;元素,然后设置它的背景。

【讨论】:

  • 你能提供更多背景信息吗?你的标记是什么样的?
  • style.background 和 style.backgroundColor 在这种情况下是相同的:jsfiddle.net/mplungjan/DukXj
  • 类“chkbox”分配给什么?如果是&lt;input type="checkbox"&gt;,则不能设置背景色。
  • 有趣,@ampersand - 那么这真的需要吗? jsfiddle.net/mplungjan/DukXj/1
  • 我一直无法更改背景颜色,@mplungian。即使您最后的小提琴也没有设置实际复选框颜色的样式,只有它周围的区域......
猜你喜欢
  • 2019-03-02
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2013-09-10
  • 2016-02-13
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多