jquery - ASP.net 标签在代码隐藏中获取值(value)

标签 jquery asp.net code-behind

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

<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 & ""

它始终是“否”,即使我知道它更改了“否”的HTML值> 到"is",以及“文本”“否”"is"

enter image description here

更改为...

enter image description here

最佳答案

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

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

html 格式

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

在 jquery 中

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

在代码后面

string strLabelVal = hdnForLabel.Value;

关于jquery - ASP.net 标签在代码隐藏中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255159/

相关文章:

java - 如何使用javascript在另一个jsp中包含一个jsp

c# - 遍历请求对象键

c# - 如何从数据库表中提取浮点值并将其分配给 asp.net C# 中的 double 类型变量?

asp.net - 在同一窗口中打开新页面

c# - 分配 Mahapps.Metro :IconPacks to button in code behind

从代码隐藏修改 Jquery Tab

JavaScript 动画功能不起作用

jQuery 未捕获类型错误 : Cannot read property 'msie' of undefined in drupal

asp.net - 代码隐藏无法引用页面控件

javascript - 为什么我不能放线性渐变?