如何更改标签的文字?

发布于 2024-09-16 09:31:48 字数 496 浏览 3 评论 0原文

我有一个单选按钮列表,单击单选按钮项目时,我必须更改其标签的文本。但由于某种原因它不起作用。代码如下:

<asp:Label ID="lblVessel" Text="Vessel:" runat="server"></asp:Label>

<script language="javascript">
  $(document).ready(function() {

    $('#rblDiv input').click(function() {
      var selected = $("#rblDiv input:radio:checked").val();
      if (selected == "exportpack") {
        $('#lblVessel').text("NewText");
      }
    });
  });
</script>

I have a radiobutton list and on click on the radio button item I have to change the text of its label. But for some reason it's not working. Code is below:

<asp:Label ID="lblVessel" Text="Vessel:" runat="server"></asp:Label>

<script language="javascript">
  $(document).ready(function() {

    $('#rblDiv input').click(function() {
      var selected = $("#rblDiv input:radio:checked").val();
      if (selected == "exportpack") {
        $('#lblVessel').text("NewText");
      }
    });
  });
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

优雅的叶子 2024-09-23 09:31:48

我遇到了同样的问题,因为我正在使用

$("#LabelID").val("some value");

我了解到您可以使用临时 jquery 方法先清除它,然后追加:

$("#LabelID").empty();
$("#LabelID").append("some Text");

或者按照惯例,您可以使用:

$("#LabelID").text("some value");

OR

$("#LabelID").html("some value");

I was having the same problem because i was using

$("#LabelID").val("some value");

I learned that you can either use the provisional jquery method to clear it first then append:

$("#LabelID").empty();
$("#LabelID").append("some Text");

Or conventionaly, you could use:

$("#LabelID").text("some value");

OR

$("#LabelID").html("some value");
时间你老了 2024-09-23 09:31:48

ASP.Net 自动为服务器端控件生成唯一的客户端 ID

将其更改为

 $('#<%= lblVessel.ClientID %>')

在 ASP.Net 4.0 中,您还可以设置 ClientIDMode属性改为Static

ASP.Net automatically generates unique client IDs for server-side controls.

Change it to

 $('#<%= lblVessel.ClientID %>')

In ASP.Net 4.0, you could also set the ClientIDMode property to Static instead.

尴尬癌患者 2024-09-23 09:31:48

试试这个:

$('[id$=lblVessel]').text("NewText");

id$= 将匹配以该文本结尾的元素,这就是 ASP.NET 自动生成 ID 的方式。您可以使用 span[id=$=lblVessel] 使其更安全,但通常这是没有必要的。

Try this:

$('[id$=lblVessel]').text("NewText");

The id$= will match the elements that end with that text, which is how ASP.NET auto-generates IDs. You can make it safer using span[id=$=lblVessel] but usually this isn't necessary.

韬韬不绝 2024-09-23 09:31:48

试试这个

$("label").html(your value);$("label").text(your value);

try this

$("label").html(your value); or $("label").text(your value);

千纸鹤 2024-09-23 09:31:48
   lable value $('#lablel_id').html(value);
   lable value $('#lablel_id').html(value);
节枝 2024-09-23 09:31:48

我自己刚刚经历过这个并找到了解决方案。请参阅 ASP.NET 标签服务器控件实际上被重新渲染为跨度(而不是输入),因此使用 .val() 属性来获取/设置将不起作用。相反,您必须在跨度上使用“text”属性并结合使用控件的 .ClientID 属性。以下代码将起作用:

$("#<%=lblVessel.ClientID %>").text('NewText');

I just went through this myself and found the solution. See an ASP.NET label server control actually gets redered as a span (not an input), so using the .val() property to get/set will not work. Instead you must use the 'text' property on the span in conjuntion with using the controls .ClientID property. The following code will work:

$("#<%=lblVessel.ClientID %>").text('NewText');
花心好男孩 2024-09-23 09:31:48

我们必须基于此找到属性值的标签标记。我们已经替换了标签文本。

脚本:

<script type="text/javascript">
$(document).ready(function() 
{ 
$("label[for*='test']").html("others");
});

</script>

Html

<label for="test_992918d5-a2f4-4962-b644-bd7294cbf2e6_FillInButton">others</label>

您想要了解更多详细信息。请单击此处

we have to find label tag for attribute value based on that.we have replace label text.

Script:

<script type="text/javascript">
$(document).ready(function() 
{ 
$("label[for*='test']").html("others");
});

</script>

Html

<label for="test_992918d5-a2f4-4962-b644-bd7294cbf2e6_FillInButton">others</label>

You want to more details .Click Here

纵情客 2024-09-23 09:31:48
<asp:RadioButtonList ID="rbtnType" runat="server">
    <asp:ListItem Value="C">Co</asp:ListItem>
    <asp:ListItem Value="I">In</asp:ListItem>
    <asp:ListItem Value="O">Out</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="lblLabelName" runat="server"></asp:Label>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<%=rbtnType.ClientID%>").change(function() {
            var rbvalue = $("input[@name=<%=rbtnType.ClientID%>]:radio:checked").val();
            if (rbvalue == "C") {
                $('#<%=lblLabelName.ClientID %>').html('text1');
            } else if (rbvalue == "I") {
                $('#<%=lblLabelName.ClientID %>').html('else text2');
            } else if (rbvalue == "O") {
                $('#<%=lblLabelName.ClientID %>').html('or elsethistext');
            }
        });
    });
</script>
<asp:RadioButtonList ID="rbtnType" runat="server">
    <asp:ListItem Value="C">Co</asp:ListItem>
    <asp:ListItem Value="I">In</asp:ListItem>
    <asp:ListItem Value="O">Out</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="lblLabelName" runat="server"></asp:Label>
<script type="text/javascript">
    $(document).ready(function() {
        $("#<%=rbtnType.ClientID%>").change(function() {
            var rbvalue = $("input[@name=<%=rbtnType.ClientID%>]:radio:checked").val();
            if (rbvalue == "C") {
                $('#<%=lblLabelName.ClientID %>').html('text1');
            } else if (rbvalue == "I") {
                $('#<%=lblLabelName.ClientID %>').html('else text2');
            } else if (rbvalue == "O") {
                $('#<%=lblLabelName.ClientID %>').html('or elsethistext');
            }
        });
    });
</script>
稀香 2024-09-23 09:31:48
$('#<%= lblVessel.ClientID %>').html('New Text');

ASP.net Label 将在浏览器中呈现为跨度。所以用户'html'。

$('#<%= lblVessel.ClientID %>').html('New Text');

ASP.net Label will be rendered as a span in the browser. so user 'html'.

宁愿没拥抱 2024-09-23 09:31:48

在 JS 中尝试一下:

单击标签文本可将其切换为替代文本,同时选择关联的输入字段并将焦点移至同一关联的输入字段。

<script type="text/javascript">"use strict";
  const oldLabelText="ORIGINAL BUTTON TAG TEXT";
  document.write(
    '<label id=labId for=inputId onClick="changeText(this.id)">'+oldLabelText+'</label>:<br>'+
    '<input id="inputId" value="The input value" />'
  );
</script>

<script type="text/javascript">"use strict";
  const stat=[oldLabelText,"NEW LABEL TEXT","THIRD LABEL TEXT","FOURTH LABEL TEXT"];
  let   cptr=0;

  function changeText(button_id){
     var el = document.getElementById(button_id);

     el.textContent = stat[++cptr % (stat.length)];

  // or
  //       el.firstChild.data = stat[++cptr % (stat.length)];

  // or
  //        el.innerHTML = stat[++cptr % (stat.length)];

  // or simply
  //        el.innerHTML = "the new text";
  }
</script>

Try this in JS:

Clicking on the label text toggles it with an alternative text, while selecting the associated input field and giving the focus to the same associated input field.

<script type="text/javascript">"use strict";
  const oldLabelText="ORIGINAL BUTTON TAG TEXT";
  document.write(
    '<label id=labId for=inputId onClick="changeText(this.id)">'+oldLabelText+'</label>:<br>'+
    '<input id="inputId" value="The input value" />'
  );
</script>

<script type="text/javascript">"use strict";
  const stat=[oldLabelText,"NEW LABEL TEXT","THIRD LABEL TEXT","FOURTH LABEL TEXT"];
  let   cptr=0;

  function changeText(button_id){
     var el = document.getElementById(button_id);

     el.textContent = stat[++cptr % (stat.length)];

  // or
  //       el.firstChild.data = stat[++cptr % (stat.length)];

  // or
  //        el.innerHTML = stat[++cptr % (stat.length)];

  // or simply
  //        el.innerHTML = "the new text";
  }
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文