简体   繁体   English

使用Ajax更改包含链接的标签内部文本

[英]Change label inner text that contains link using Ajax

I have a label that contains a link like following: 我有一个包含链接的标签,如下所示:

<label id="textlabel" > <a href="#"  id="testlink">data</a> </label>

I am trying to use Ajax to change label text, but it does not work. 我正在尝试使用Ajax来更改标签文本,但是它不起作用。

For testing purposes, it works here but it does not work on my web page (the web page is brand new without anything else). 出于测试目的,它在这里可以使用,但是不能在我的网页上使用(该网页是全新的,没有任何其他内容)。

$('#testlink').click(function(){
    $('#testlabel').text("new data");
});

JavaScript: JavaScript的:

function myfunc(clicked_id) {

            var label = document.getElementById(clicked_id).parentElement;
            var params = "{'orderid':'" + clicked_id + "'}";
            var fd = new FormData();
            fd.append("orderid", params);
            alert("test");

            $("'#" + clicked_id + "'").click(function () {
                $.ajax
                       ({
                           url: 'Handler.ashx',
                           data: fd,
                           processData: false,
                           contentType: false,
                           type: 'POST',
                           success: function (result) {
                               $("'#"+label.id+"'").text(result);
                               }
                       })
            });
        }

Update: 更新:

  1. the following ajax works in general 以下ajax可以正常使用

      <script type="text/javascript"> function myfunc(clicked_id) { var params = "{'orderid':'" + clicked_id + "'}"; var label = document.getElementById(clicked_id).parentElement; $("#"+label.id).click(function () { $.ajax({ type: "POST", url: "Handler.ashx", data: clicked_id, success: function (result) { $("#" + label.id).text(result); } }); }); } 

  2. i spent so much time to pinpoint issue and now i think the issue is found at autogenerated elements. 我花了很多时间来查明问题,现在我认为问题是在自动生成的元素上发现的。 for example 例如

     <table> <tr><td><label id="lbl" style="background-color:yellow"> <a href="#" onclick="myfunc(this.id)" id="00000">Label</a> </label></td></tr> @for(int i=0;i<4;i++) { <tr><td> <label id="lbl+@i" style="background-color:yellow"> <a href="#" onclick="myfunc(this.id)" id="@i">Label</a> </label></td></tr> } </table> 

ajax only changes first label' text but not other auto generated links and labels. Ajax仅更改第一个标签的文本,而不更改其他自动生成的链接和标签。 the following part does not run when clicking on auto generated links 单击自动生成的链接时,以下部分无法运行

            $("#"+label.id).click(function () {
            $.ajax({
                type: "POST",
                url: "Handler.ashx",
                data: clicked_id,
                success: function (result) {
                    $("#" + label.id).text(result);
                          }          
            }); });

Update: 更新:

Finally I found what went wrong. 终于,我发现出了什么问题。 it is the label ids I used. 这是我使用的标签ID。 they all have "+" after changing to "_", my app works fine now. 它们都改为“ _”后都带有“ +”,我的应用现在可以正常工作了。

thanks to all who helped 感谢所有帮助的人

Update after the question's code was changed 更改问题代码后更新

There's a glitch in 有一个小故障

success: function (result) {
    $("'#"+label.id+"'").text(result) 
}

should be 应该

success: function (result) {
    $("#"+label.id).text(result) 
}

Also, to test if the label object is there, you could add 另外,要测试标签对象是否存在,您可以添加

success: function (result) {
    console.log(label); // DON'T FORGET TO REMOVE THIS LINE LATER BEFORE DEPLOYING
    $("#"+label.id).text(result) 
}

Maybe because in your test function you've misspelled the label's id textlabel as testlabel 可能是因为在您的测试函数中,您将标签的id textlabel 拼写错误testlabel

in

$('#testlink').click(function(){
    $('#testlabel').text("new data");
});

?

Then in your real code: 然后在您的真实代码中:

success: function (result) {
    $('#label.id).text(result);
}

#label.id is #label.id

  1. not the correct jQuery selector. 不是正确的jQuery选择器。 It should be '#textlabel' 它应该是'#textlabel'
  2. It's missing the closing ' 它缺少结尾“

Hey If you want to update all label text containing links please try this code I also update your demo link please refer 嘿,如果您想更新所有包含链接的标签文本,请尝试此代码,我也将更新您的演示链接,请参考

$('[id*="test"]').text("ddaadfa");

See Demo 观看演示

If you are using ajax then you have to use this code in success block to change text of link 如果您使用的是ajax,则必须在成功块中使用此代码来更改链接的文本

success: function (result) {
    $('[id*="test"]').text("ddaadfa");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM