【发布时间】: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
-
@Tim 看看这里jsfiddle.net/mplungjan/DukXj/1
-
谢谢。我添加了所有使用的代码。
-
您能否发布发送到浏览器的 html(查看源代码),因为这是 JavaScript 使用的。另外:您的“查询”标签是指“jquery”吗? (不建议您应该使用 jQuery,但您似乎正在使用它,并且“查询”标签似乎与您的问题无关。)
标签: javascript jquery checkbox