表格单元格的onClick选择单元格内的单选按钮 [英] onClick of table cell selects radio button inside the cell
问题描述
@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屋!