问题描述:

Code below hides table row based on checkbox checked state.

I have been unable to target the id of an asp:checkbox because of the way they render.

CODE (Table wrapped in datalist)

<table>

<tr>

<td><asp:CheckBox ID="cb" CssClass="css" RunAt="Server"/></td>

</tr>

<tr class="row"><td></td</tr>

</table>

SCRIPT

$("#<%= dl.ClientID %> #<%= cb.ClientID %>").change(function(){

var hide = this.checked ? 'none' : 'table-row';

$('.row').css('display', hide);

});

SOLUTIONS

$("#<%= dl.ClientID %> input:checkbox").change

(Works but does not explicitly target a unique checkbox)

$("#<%= dl.ClientID %> .css").change

(Does not work because asp:checkbox class is attatched to a wrapped span tag)

$("#<%= dl.ClientID %> #<%= cb.ClientID %>").change

*(Does not work because asp:checkbox id renders suffux of _0 the checked state)*

THE ANSWER

<asp:CheckBox ID="cb" RunAt="Server" onclick="hide();"/>

function hide(){

$('.row').toggle()

};

网友答案:

I know that the focus is always on unobtrusive javascript, but you always have the option to add the onclick attribute to the checkbox if you can't figure out a better solution.

<asp:CheckBox ID="cb" CssClass="css" RunAt="Server" onclick="callfunction(this);/>
网友答案:

Try setting the ClientIDMode to Static. This sets the ClientID to the value of the ID.

<asp:CheckBox ID="cb" ClientIDMode="Static" RunAt="Server" onclick="hide()" />

function hide() {
    $('#cb').toggle();
}
相关阅读:
Top