首先,我想说我不是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(',');
}
});
}
虽然可以从“ click”事件中获取活动链接的文本,但无法从第二个函数中获取。
该应用程序的工作方式是,第一个函数调用ajax c#文件,然后该文件将成功返回第二个jquery函数,并带有c#的一些结果。
我需要将c#返回的结果与单击的超链接内部的内容进行比较,但是无法从该“ AjaxSuccess”函数中获取单击的文本。
最佳答案
当您使用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(',');
}
});