从 jquery 访问内容页面元素

发布于 2024-12-09 12:07:36 字数 472 浏览 1 评论 0原文

我正在尝试从 jquery 访问文本框和隐藏字段的值。我得到的值是未定义的。

这些控件位于我的母版页的内容页面内。

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

在 javascript 函数中:

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

提前感谢

BB

I am trying to access the value of 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

唯憾梦倾城 2024-12-16 12:07:37

您还没有提到 ClientIDMode用于datepickerContact 控件。默认情况下,该模式将是 Predicatable(如果是迁移的站点,则为 AutoID),并且它将使用父命名容器的串联生成 html side id,在内容页面的情况下,该父命名容器将是内容占位符。

简而言之,datepickerContact 文本框将具有类似于“content1_datepickerContact”的 html id 属性,因此 jquery 选择器将找不到 html 控件。

简单的解决方案是将 ClientIDMode 用作静态。例如,

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

另一种方法是传递通过 ClientID 属性到您的 java 脚本函数。如果您的函数是在标记 (aspx) 上定义的,那么您可以使用服务器端指令将客户端 ID 嵌入到脚本中,如 rick schott 所示。如果您的函数位于外部 js 中,那么您必须将客户端 ID 作为参数传递。

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.

霓裳挽歌倾城醉 2024-12-16 12:07:37
$(document).ready(function() {
    var Insid = $("#<%= datepickerContact.ClientID %>");
    var firstcontactDate = $("#IAssignmentId");
    alert(Insid.val());
    alert(firstcontactDate.val());
});
$(document).ready(function() {
    var Insid = $("#<%= datepickerContact.ClientID %>");
    var firstcontactDate = $("#IAssignmentId");
    alert(Insid.val());
    alert(firstcontactDate.val());
});
清旖 2024-12-16 12:07:37

您可以通过编写此

var insid=$("#<%= datepickerContact.ClientID %>").val();
来访问
警报(内部);

you can access by writing this

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文