我有一个带输入元素的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()更多推荐
发布评论