开发者

Access content page elements from jquery

开发者 https://www.devze.com 2023-04-12 22:09 出处:网络
I am trying to access the value o开发者_StackOverflow中文版f a textbox and Hidden field from jquery.I am getting value as undefined.

I am trying to access the value o开发者_StackOverflow中文版f a textbox and Hidden field from jquery. I am getting value as undefined.

These controls are inside the content page of my master page.

 <input id="datepickerContact" type="text" runat="server" />
<input type="hidden" runat="server" id="IAssignmentId" clientidmode="Static" />

In the javascript function :

 var Insid = $("#datepickerContact");
    var firstcontactDate = $("#IAssignmentId");
    alert(Insid.val());
    alert(firstcontactDate.val());

Thanks in advance

BB


You haven't mentioned the ClientIDMode for datepickerContact control. By default, the mode would be Predicatable (or AutoID if a migrated site) and it will generate the html side id using concatenation of parent naming container which will be content placeholder in case of content pages.

In short, datepickerContact textbox will have html id attribute as something similar to "content1_datepickerContact" and hence jquery selector would not find the html control.

The simple solution is to use ClientIDMode as static. For example,

<input id="datepickerContact" type="text" runat="server" ClientIDMode="Static" />

Yet another way is to pass the actual client id (irrespective of client id mode) obtained via ClientID property to your java-script function. If your function is defined on the markup (aspx) then you can use server side directive to embed client id in script as illustrated by rick schott. If your function lies in external js then you have to pass the client id as parameter.


$(document).ready(function() {
    var Insid = $("#<%= datepickerContact.ClientID %>");
    var firstcontactDate = $("#IAssignmentId");
    alert(Insid.val());
    alert(firstcontactDate.val());
});


you can access by writing this

var insid=$("#<%= datepickerContact.ClientID %>").val();
alert(insid);

0

精彩评论

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

关注公众号