如何引用一个TextBox在JavaScript中的ASP.NET控件中? [英] How to refer to a TextBox in JavaScript inside an ASP.NET control?

查看:95
本文介绍了如何引用一个TextBox在JavaScript中的ASP.NET控件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些文本字段和复选框ASP.NET控件。我想修改使用JavaScript基于页面加载后的值present在文本字段中的复选框状态。

I have an ASP.NET control with some text fields and a checkbox. I would like to modify the checkbox state using JavaScript based on the values present in the text fields after page load.

我如何可靠地读取JavaScript中的文本字段值?只见这个答案,但它并没有帮助:

How can I reliably read the text field values in JavaScript? I saw this answer but it didn't help:

<asp:TextBox Text='<%# Bind("something") %>' ID="txtSomething" runat="server">
</asp:TextBox>

<script type="text/javascript">
    alert($('#<%= txtSomething.ClientID %>').val());
</script>

这不工作; VS告诉我, txtSomething 未声明,它可能无法访问由于其保护级别。

This doesn't work; VS tells me that "txtSomething is not declared. It may be inaccessible due to its protection level."

我还能有别的可以参考该文本框在JavaScript中?

How else can I refer to that text box in JavaScript?

推荐答案

您可以尝试取得控制权的客户端ID,其中包含文本框和他的孩子们输入搜索之后,ID包含文本框服务器ID

You can try to get client ID of control which contains your text box and after search in his children input which ID contains your text box server ID

<script type="text/javascript">
    alert($('#<%= parentControl.ClientID %>').find("input[id$='txtSomething']").val());
</script>

作为另一种选择,你可以这样做:

As another option you can do:

<script type="text/javascript">
    alert($('#<%= (parentControl.FindControl("txtSomething")).ClientID %>').val());
</script>

这篇关于如何引用一个TextBox在JavaScript中的ASP.NET控件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆