简体   繁体   English

单击更改表格行的背景颜色

[英]Change background color of a table row on click

On my webpage, I have a table and I'd like to change the background of a row, and in the process, check the radio button corresponding to that row when the row is clicked. 在我的网页上,我有一张桌子,我想更改行的背景,并在此过程中,单击该行时检查与该行相对应的单选按钮。 I tried the following using jquery : 我尝试使用jquery进行以下操作:

<style>
  .active { background-color: #8fc3f7;}
</style>  
<script>
$(document).ready(function() 
  {
    $('#reqtablenew tr').click(function () 
       {
         $('#reqtablenew tr').removeClass("active");
         $(this).addClass("active");
       });
  });
<script>

Any thoughts as to what I might be doing wrong or any workaround would be very welcome. 对于我可能做错了什么或任何解决方法的任何想法都将受到欢迎。 I have included the following scripts in the page. 我在页面中包含了以下脚本。 src="http://code.jquery.com/jquery-1.9.1.js"> and src="http://code.jquery.com/ui/1.10.3/jquery-ui.js" src =“ http://code.jquery.com/jquery-1.9.1.js”>和src =“ http://code.jquery.com/ui/1.10.3/jquery-ui.js”

Here's a fiddle of my table http://jsfiddle.net/Gz668/5/ 这是我的桌子上的小提琴http://jsfiddle.net/Gz668/5/

You forgot to add jquery 您忘记添加jquery

Add this in you css 在您的css中添加

tr.active td {
   background-color: #8fc3f7;
}

I have updated your demo to this 我有你的演示更新到

Updated to check the radio try this, 更新以检查radio试试这个,

$(this).find('[name="reqradio"]').prop('checked',true);

Full Code 完整代码

$(document).ready(function () {
    $('#reqtablenew tr').click(function () {
        $('#reqtablenew tr').removeClass("active");
        $(this).addClass("active");
        $(this).find('[name="reqradio"]').prop('checked',true);
    });
});

Demo 演示版

The problem is the td got background color too 问题是td也有背景色

.newreqtable td {
   .....
   background-color:#ffffff;

So you need to enforce your active class, eg 因此,您需要强制执行活动类,例如

tr.active td{background-color: #8fc3f7;}

See this: http://jsfiddle.net/Gz668/9/ 看到这个: http : //jsfiddle.net/Gz668/9/

Something like this ... : 像这样的东西...:

$(this).find('input').prop('checked', true);

.. will check the radiobutton of the clicked row. ..将检查单击的行的单选按钮。

[Fiddle] ( http://jsfiddle.net/McNull/LwT9N/ ) [小提琴]( http://jsfiddle.net/McNull/LwT9N/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM