嘿,我正在尝试从代码中的标签获取值:
<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"
更改为...
最佳答案
标签值不会回传,您必须使用隐藏字段。您可以使用标签在客户端浏览器上显示值,但要在回发时发送值,您需要使用隐藏字段。
您可以使用 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/