javascript - 实时更改中继器内的标签
问题描述
我正在尝试根据也在转发器内的文本框的值实时更改转发器内的标签。
我曾尝试使用 JS 解决此问题,但无法访问标签或中继器内的文本框。
如果标签或文本框在中继器之外,我可以毫无问题地访问它
JS:
<script language="javascript" type="text/javascript">
document.getElementById('<%=labelToUpdate.ClientID%>').innerHTML = document.getElementById('<%=txtInput.ClientID%>').value;
</script>
ASPX:
<asp:TextBox ID="txtInput" runat="server" class="form-control"></asp:TextBox>
<asp:Label ID="labelToUpdate" runat="server"></asp:Label>
任何帮助总是很感激。
谢谢
解决方案
中继器控件位于 ItemTemplate 内,因此您不能直接引用它们。您需要通过索引访问它们。
用你的方法你可以做到这一点
document.getElementById('<%= Repeater1.Items[i].FindControl("labelTotalCsrPercentage").ClientID %>').innerHTML =
document.getElementById('<%= Repeater1.Items[i].FindControl("txtInput").ClientID %>').value;
或者使用 jquery 时变得更容易。然后您可以使用nth-of-type
选择器
$('#MyRepeater1 span:nth-of-type(2)').html($('#MyRepeater1 span:nth-of-type(2)').prev('input').val());
为此,您需要使用具有唯一 ID 的元素包装中继器
<span id="MyRepeater1">
<asp:Repeater ID="Repeater1" runat="server">
</asp:Repeater>
</span>
推荐阅读
- c# - 当我在工作表中写入日期时,C# Excel 会更改日期格式
- javascript - 使用基于文本的javascript设置元素的颜色
- javascript - 将导入的对象从父级传递给子级作为道具与在这些子级中导入该对象的权衡是什么?
- vue.js - Vuex getter 总是返回 null
- python - 对象和引用的 ID 函数
- c# - 如何在 Unity 中基于相机视口将纹理应用于材质
- php - Redis 和 symfony PSR16 缓存
- python - 如何将字段的“描述”放在表单字段中?
- php - 如何处理可排队通知上的失败作业
- ios - segue 后页面视图大小不正确