onClick of table cell selects radio button inside the cell

二次信任 提交于 2019-12-12 04:09:23

问题


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?


回答1:


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)
    });


来源:https://stackoverflow.com/questions/36829880/onclick-of-table-cell-selects-radio-button-inside-the-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!