1

嘿,我正在尝试从代码中的标签中获取值:

<div id="chkHaz" data-checked="no">
   <asp:Label ID="lblchkHaz" runat="server" Text="no" ClientIDMode="Static" Style="visibility: hidden; display: none;"></asp:Label>
   <asp:Image ID="check_chkHaz" runat="server" ImageUrl="~/images/chkOFF.png" ClientIDMode="Static" />
</div>

我根据用户是否通过 JQuery “检查”了它来设置它:

$("#chkHaz").click(function (input) {
   if ($(this).attr("data-checked") == "no") {
      $('#check_' + $(this).attr('id')).attr("src", "/images/chkON.png");
      $(this).attr("data-checked", "yes");
      $('#lbl' + $(this).attr('id')).attr("text", "yes");
      $('#lbl' + $(this).attr('id')).html("yes");
   } else {
      $('#check_' + $(this).attr('id')).attr("src", "/images/chkOFF.png");
      $(this).attr("data-checked", "no");
      $('#lbl' + $(this).attr('id')).attr("text", "no");
      $('#lbl' + $(this).attr('id')).html("no");
   }
});

但是,当我通过后面的代码检查它时:

Dim strChkHaz As String = lblchkHaz.text & ""

即使我知道它将“no”的HTML 值更改为“yes”并且将“text”“no”更改为 yes”,它始终是“no ”

在此处输入图像描述

更改为...

在此处输入图像描述

4

3 回答 3

2

标签值不会回发,您必须使用隐藏字段。您可以使用标签在客户端浏览器上显示值,但要在回发时发送值,您需要使用隐藏字段。

您可以使用 input、type="hidden" 或 asp:hidden 字段来检索标签的值。

在 html 中

<input type="hidden" runat=server ID="hdnForLabel" />

在jQuery中

$('<%= hdnForLabel %>').value = "some value";

在后面的代码中

string strLabelVal = hdnForLabel.Value;
于 2012-11-06T16:22:23.833 回答
1

1)在 .aspx 文件中添加

<asp:HiddenField runat=server ID="..." />

2)在JS中找到隐藏字段并在更改标签时同时更新它。

3) 现在在代码隐藏中读取隐藏字段中的 .Value 属性,而不是查看标签文本。

于 2012-11-06T16:25:11.873 回答
0

看起来您的选择器首先是错误的。当runat="server"应用于 ASP.NET 控件时,id 会与 container 一起添加。所以你的 id 看起来与 HTML 中的不一样。所以在这种情况下,你需要使用属性以开头或属性有选择器

$('#check_' + $(this).attr('id'))

必须

$('[id*="check_' + $(this).attr('id') + '"]')
于 2012-11-06T16:26:03.627 回答