Javascript 无法从jquery设置标签文本

Javascript 无法从jquery设置标签文本,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,要求是当用户选择任何节点时,从树状视图设置标签文本。调试时,我正确获取hospitalName,但它没有设置标签文本 $("[id$=HospitalTreeViewDiv] span").click(function () { var hospitalName = $(this)[0].outerText; //this is also not working // $("label[for='SelectedHospitalLiteral']").text(hospitalN

要求是当用户选择任何节点时,从树状视图设置标签文本。调试时,我正确获取hospitalName,但它没有设置标签文本

$("[id$=HospitalTreeViewDiv] span").click(function () {

  var hospitalName = $(this)[0].outerText;

  //this is also not working
  // $("label[for='SelectedHospitalLiteral']").text(hospitalName); 

  $("[id$=SelectedHospitalLiteral]").val("abc");

});
这是aspx代码段

   <div runat="server" id="HospitalTreeViewDiv">
            <asp:TreeView ID="HospitalTreeView" runat="server" ExpandDepth="0">
            </asp:TreeView>
   </div>

   <div class="alignBottom">
        <p> <asp:Label ID="SelectedHospitalLiteral" runat="server" ></asp:Label></p>
   </div>


使用HiddenField进行回发

<asp:HiddenField runat="server" ID="labelText" Value=""/>

$("[id$=HospitalTreeViewDiv] span").click(function () {
      $("[id$=labelText]").val("abc")
});

使用
asp控件标签控件时
使用
ClientID
获取控件:

还要设置文本,请使用
text(“yourtext”)

在此处使用您的代码:

 $("[id$=HospitalTreeViewDiv] span").click(function () {

      var hospitalName = $(this)[0].outerText; //your forgot to give semicolon here

      $("#<%=SelectedHospitalLiteral.ClientID%>").text("abc"); // and also semicolon  here 

    });
$(“[id$=HospitalTreeViewIV]span”)。单击(函数(){
var hospitalName=$(this)[0].outerText;//您忘记在这里输入分号了
$(“#”).text(“abc”);//这里还有分号
});

对于
.text(“您的文本”)
请以
结束语句
.val()
仅用于具有
值的元素,而
标签的情况并非如此。改为使用
.text()
.html()
没有帮助。短暂的微笑
$html
在UI上工作,但在code behind(代码隐藏)下,我仍然会得到旧值你说的代码隐藏是什么意思:一旦你为标签设置了文本,你就可以在code behind(代码隐藏)中访问该值…#不,我检查了$html是否正在更改文本,但当你看到标签时。它后面的文本代码会给出旧值(在我的情况下,它是空的)因此,我得到了这个呈现的html
您是发回您的页面,还是将其重定向到同一页面。因为当你想访问代码隐藏中的文本时,如果它没有被发回,它将重新呈现HTML并重新设置labelI am new to asp.net的文本,让我分析一下你最后的评论在我的例子中它崩溃了。
本身。。它说
Microsoft JScript运行时错误:语法错误,无法识别的表达式:#
yes-ur-r-right它将崩溃。渲染ID将是多个,因此…修改了ans