【问题标题】:Get Text of clicked link from a function从函数中获取点击链接的文本
【发布时间】:2015-07-15 19:07:27
【问题描述】:

首先我想说我不是专家,甚至可能不是 jQuery 的中级用户,感谢任何和所有帮助。

这是我的小问题的模拟:

https://jsfiddle.net/c897enhy/

<a id="someRandomID_hypNotifSentTo_0" class="NotifSent2" href = "#">
    this is the text i want to get
</a>
<br>
<a id="someRandomID_hypNotifSentTo_0" class="NotifSent2" href = "#">
    this is the text i want to get 2
</a>
<br>
<a id="someRandomID_hypNotifSentTo_0" class="NotifSent2" href = "#">
    this is the text i want to get 3
</a>


if ($) {
    $(document).ready(function () {

        $("a[id*='_hypNotifSentTo_']").live("click", function ($e) {

            // Will post list of recipients to be removed from the file
            var x = $(this).text();
            alert(x);
            AjaxSuccessPopulateRecipients("restult");
        });

         function AjaxSuccessPopulateRecipients(result) {
             alert("asdf");
             var x = $('#NotifSent2').toString();
             alert(x);
             var x = $(this).text();
             alert(x);

             var recipients = $(this).text();
             alert("1");
             var recipientArr = recipients.split(',');
         }
       });
}

虽然我可以从“点击”事件中获取活动链接的文本,但我无法从我的第二个函数中获取。

应用程序的工作方式是,第一个函数调用 ajax c# 文件,然后将成功返回到第二个 jquery 函数,其中包含 c# 的一些结果。

我需要将从 c# 返回的结果与单击的超链接内的结果进行比较,但无法从“AjaxSuccess”函数中获取单击的文本。

【问题讨论】:

  • var x = $('#NotifSent2').toString(); var x = $(this).text(); 这完全没有意义。

标签: javascript c# jquery


【解决方案1】:

当您在 Ajax 函数中时,您将丢失 $(this) 的上下文($(this) 将不再引用单击的链接)。尝试添加一个可以存储上下文的变量,如下所示:

$(document).ready(function () {
var $that;
        $("a[id*='_hypNotifSentTo_']").live("click", function ($e) {

            // Will post list of recipients to be removed from the file
            $that = $(this);
 var x = $that.text();
            alert(x);
            AjaxSuccessPopulateRecipients("restult");
        });

         function AjaxSuccessPopulateRecipients(result) {
alert("asdf");
             //var x = $('#NotifSent2').toString();
              //alert(x);
            var x = $that.text();
            alert(x);

            var recipients = $(this).text();
            alert("1");
            var recipientArr = recipients.split(',');
         }
       });

【讨论】:

  • 谢谢!我会再测试一些,但看起来它正在工作!
【解决方案2】:

AjaxSuccessPopulateRecipients 内的$(this) 指的是寡妇对象,而不是被点击的锚标记。

只需将锚标记的引用从点击事件发送到第二个函数,就像这样

AjaxSuccessPopulateRecipients("restult", $(this));

像这样使用它作为上下文

function AjaxSuccessPopulateRecipients(result, context) { // <--- context refers to the anchor tag
  alert("asdf");
  var x = $('.NotifSent2').text();
  alert(x);
  var x = context.text();
  alert(x);
  var recipients = context.text();
  alert("1");
  var recipientArr = recipients.split(',');
}

【讨论】:

  • 对寡妇和家属表示由衷的同情! ;)
  • @Andreas:哈哈! :D 不仅如此,Window 也被严重滥用。
【解决方案3】:

看看Function.prototype.call()

call() 方法调用具有给定 this 值的函数,并且 单独提供的参数。

变化:

AjaxSuccessPopulateRecipients("restult");

收件人:

AjaxSuccessPopulateRecipients.call(this, "restult");

这样做会将正确的 this 值传递给您的函数。

【讨论】:

  • 其他一些答案建议传入this$(this) 作为附加参数。使用call 可以实现类似的效果,但优点是无需担心额外的参数或更改现有代码。
【解决方案4】:

您不应通过此 $('#NotifSent2') 选择器访问数据,因为 # 选择器是用于在页面上查找 ID 的 JQuery 语法,而您的元素是使用 class="NotifSent2" 构造的。

如果您希望从不同的函数访问变量,则必须确保该变量在正确的范围内。

由于您的 AJAX 调用和 AjaxSuccessPopulateRecipients() 函数在同一范围内,因此您无法跨两者访问 $(this)。

只需将您希望使用的变量传递给您的函数,如 $(this) AjaxSuccessPopulateRecipients("restult", $(this));

【讨论】:

    猜你喜欢
    • 2019-05-06
    • 2011-06-19
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2012-11-28
    • 1970-01-01
    相关资源
    最近更新 更多