表格单元格的onClick选择单元格内的单选按钮 [英] onClick of table cell selects radio button inside the cell

查看:103
本文介绍了表格单元格的onClick选择单元格内的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有14行5 td单元。每一个都是一个单选按钮。我希望用户能够单击表格单元格中的任意位置以选择其单选按钮。每一行都是这样创建的:

  @for(int i01 = 1; i01 <= 5; i01 ++){
< td id =tablecellradiobuttononMouseover =this.bgColor = '88,150,183'onMouseout =this.bgColor ='white'onclick =@('Q01'+ i01).checked = true>
@ Html.RadioButton(Q01,i01,Q01 == i01.ToString(),new {id =Q01+ i01})
< / td>
}

然而,我的onClick正在崩溃,给我错误:

 编译器错误消息:CS1012:字符字面值太多字符

如果没有JavaScript(或jQuery)函数,这确实应该很简单,但我认为我已经放弃了需要一个。但有人可以帮我纠正我的代码吗?

解决方案

我无法更正您现有的代码,但您将拥有
$ b

 < td id =tablecellradiobutton... 

您需要为每个td创建单独的id - 可能将tr和td索引编号附加到现有id将会成为问题。



我会使用jQuery并使用find()获取td内被点击的输入并将其checked属性设置为true。请注意document.on的使用,这将允许动态添加内容以及静态内容。不要忘记$(document).ready ...如果你还没有它;

  $( (''checked',true)$ b $(this) $ b}); 


I have 14 rows of 5 td cells. In each one is a radio button. I want the user to be able to click anywhere in the table cell in order to select its radio button. Each row is created thus:

                @for (int i01 = 1; i01 <= 5; i01++) {
                <td id="tablecellradiobutton" onMouseover="this.bgColor='88,150,183'" onMouseout="this.bgColor='white'" onclick="@('Q01' + i01).checked = true">
                    @Html.RadioButton("Q01", i01, Q01 == i01.ToString(), new { id = "Q01" + i01 })  
                </td>
                }

However, my onClick is falling over, giving me the error:

Compiler Error Message: CS1012: Too many characters in character literal

This really ought to be damn simple to do without a JavaScript (or jQuery) function, but I think I'm becoming resigned to needing one. But could someone help me to correct my code?

解决方案

I can't correct your existing code, but you are going to have multiple instances of thesame id:

 <td id="tablecellradiobutton"...

you need to create individual ids for each td - probably appending the tr and td index number to the existing id would be the go.

I would use jQuery and using find() get the input within the td that was clicked and set its checked property to true. Note the use of the document.on... which will allow dynamically added content to be targeted as well as static content. Don't forget the $(document).ready... if you don't already have it.;

    $(document).on('click','td',function(){
    $(this).find('input [type="radio"]').prop('checked',true)
    });

这篇关于表格单元格的onClick选择单元格内的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆