开发者

using jquery in two usercontrol in the same page

开发者 https://www.devze.com 2023-02-16 10:08 出处:网络
I have two user control in my page uc1 and uc2. I want to make sure that the js function inside thes开发者_JAVA技巧e two user control fire when the document is ready.

I have two user control in my page uc1 and uc2. I want to make sure that the js function inside thes开发者_JAVA技巧e two user control fire when the document is ready.

When I am using '$(document).ready({function(){//something});' in both the user control only the function of the first user control is loading. the function for the second user control was not called.

Can't I use '(document).ready' in two different user control in the same page? If not then how can I make sure that the respective methods are called only when the document is ready?


You can add any number of functions to $(document).ready, and they should all execute.

Perhaps the 2nd script gives an error before it can do what it's supposed to do? Consider installing FireBug and open your website in FireFox.


you can do any of the below

1

structure of your .ascx file

    //Demo control
    Name : <asp:TextBox ID="txtName" />

Javascript

//this is DOM ready
jQuery(function(){
   jQuery('#'+<%=txtName.ClientID%>).doSomething();
});

2 or other way

<asp:TextBox ID="txtName" CssClass="myClass" />

add this code the page that holds these controls

jQuery(function(){
    jQuery('.myClass').DOSomeThing();
});


My first bet would go for checking duplicate namings of functions, variables that has same name. I woukd also figure out if i can put functionality to tha masterpage (or a new, if its specifik). This would simplify error tracing. I also recommend firebug andhttpwatch for js debugging. Now your sample gaves a small indication that document.ready is the only method call u are using in each user control?

0

精彩评论

暂无评论...
验证码 换一张
取 消