Domanda

I have a checkbox in my formview which I want to access in JS to do some enable/disable for textboxes. This is what I have:

 <script type="text/javascript">
     $(document).ready(function () {              
         $('#<%=FormView1.FindControl("chkMap").ClientID%>').change(function () {
             if ($(this).is(":checked")) {                                               
             }
             $('#textbox1').val($(this).is(':checked'));
         });             
 });

With this code, nothing happens and no error is shown in firebug console. Am I doing something wrong? Thanks, Laziale

È stato utile?

Soluzione

I have a few guesses. The first one is probably wrong but I had to point it out. The code you posted does not have an end script (</script>) tag. You should check that first.

Second guess is, for some reason <%=FormView1.FindControl("chkMap").ClientID%> this might not be outputting the ID of that control. Have you tried to 'view source' of that page and made sure that the ID is as it should be? Maybe you've changed the ID of that checkbox or something.

Third guess is the way you reference the textbox (#textbox). This is an ASP.NET WebForms page. Your checkbox is a server control (<asp:CheckBox ... />). Are you sure you haven't created the textbox like <asp:TextBox runat="server" ID="textbox1" />? If you have, you would need to write the code like:

$('#<%=FormView1.FindControl("textbox1").ClientID%>').val($(this).is(':checked'));

to reference that textbox control via JavaScript.

I'm suggesting all these because the JS code works fine. I've created a JSFiddle and tested it.

Here's the fiddle. link

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top