在不更改元素id的情况下更改input元素的值(Changing value of input element without changing element id)

编程入门 行业动态 更新时间:2024-10-28 01:23:35
在不更改元素id的情况下更改input元素的值(Changing value of input element without changing element id)

我有一个带输入元素的ASPX页面。 在页面加载时,我设置该输入元素的值。 稍后我尝试使用元素id(使用javascript)获取该字段的内容,但由于id已被更改,因此失败。 有没有办法阻止C#更改元素ID?

输入元素:

<input id="conferenceDate" runat="server" style="width:100%;" />

在Page_Load函数中设置日期:

conferenceDate.Value = DateTime.Now.ToString();

元素值之后的id已设置:

<input name="ctl00$PlaceHolderMain$conferenceDate" type="text" id="ctl00_PlaceHolderMain_conferenceDate" style="width:100%;" value="7/21/2014 11:30:55 AM" />

更新:我尝试了以下但是仍然没有改变id的工作:

<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />

当我稍后尝试使用jQuery选择它们时,我必须使用更改的id:

$("ctl00_PlaceHolderMain_conferenceDate").val()

I have an ASPX page with an input element. On page load I set the value of that input element. Later on I try to get the contents of that field using the elements id(using javascript) but it fails because the id has been changed. Is there any way to stop C# from changing the elements id?

input element:

<input id="conferenceDate" runat="server" style="width:100%;" />

Setting the date in the Page_Load function:

conferenceDate.Value = DateTime.Now.ToString();

The id after the element vale has been set:

<input name="ctl00$PlaceHolderMain$conferenceDate" type="text" id="ctl00_PlaceHolderMain_conferenceDate" style="width:100%;" value="7/21/2014 11:30:55 AM" />

UPDATE: I have tried the following but neither work the id is still changed:

<asp:TextBox ID="conferenceDate" runat="server" ClientIDMode="Static" />

and

When I try to select them later with jQuery I have to use the altered id:

$("ctl00_PlaceHolderMain_conferenceDate").val()

最满意答案

除了将ClientIDMode设置为static ,您还可以使用ClientID属性查找id 。

<%= conferenceDate.ClientID %>

使用您的示例信息,您可以这样找到:

单引号或双引号,不要忘记哈希标记(如果它是一个类,则为点)。

$("#<%= conferenceDate.ClientID %>").val()

In addition to setting the ClientIDMode to static, you can also find the id using the ClientID property.

<%= conferenceDate.ClientID %>

Using your sample information you can find it like this:

Single or double quotes and dont forget the hash mark (or dot if its a class).

$("#<%= conferenceDate.ClientID %>").val()

更多推荐

本文发布于:2023-07-30 06:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1336762.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   情况下   id   input   element

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!