Jquery 如何在ajax调用中访问ActionLink url参数值?
我试图从ActionLink中获取一个参数值,以便在ajax调用中发送它。我的脚本如下所示:Jquery 如何在ajax调用中访问ActionLink url参数值?,jquery,asp.net-mvc,asp.net-ajax,Jquery,Asp.net Mvc,Asp.net Ajax,我试图从ActionLink中获取一个参数值,以便在ajax调用中发送它。我的脚本如下所示: $("a.studentName").on("click", function () { var linkID = this.id; var theProp = $("linkID").attr("href"); alert(linkID + "" + theProp); $.ajax({ type: "G
$("a.studentName").on("click", function () {
var linkID = this.id;
var theProp = $("linkID").attr("href");
alert(linkID + "" + theProp);
$.ajax({
type: "GET",
url: "/Controller/Action",
data: { "data": linkID },
dataType: "html",
success: function (data) {
$("theTimes").html(data);
}
});
});
@Ajax.ActionLink(stdFName, "Action", "Controller", new { studentNumber = stdNum }, null, new { @class = "studentName", id = "linkNo" + appendId.ToString() }); @:
<a id="linkNo1" class="studentName" href="/Controller/Action?studentNumber=172" data-ajax="true">Gary</a>
var theProp = $("linkID").attr("href");
alert(linkID + "" + theProp);
它呈现如下所示的html:
$("a.studentName").on("click", function () {
var linkID = this.id;
var theProp = $("linkID").attr("href");
alert(linkID + "" + theProp);
$.ajax({
type: "GET",
url: "/Controller/Action",
data: { "data": linkID },
dataType: "html",
success: function (data) {
$("theTimes").html(data);
}
});
});
@Ajax.ActionLink(stdFName, "Action", "Controller", new { studentNumber = stdNum }, null, new { @class = "studentName", id = "linkNo" + appendId.ToString() }); @:
<a id="linkNo1" class="studentName" href="/Controller/Action?studentNumber=172" data-ajax="true">Gary</a>
var theProp = $("linkID").attr("href");
alert(linkID + "" + theProp);
但是我只得到id的值,而不是url参数的值。你能帮我访问参数值吗?谢谢你对这个的帮助 看看你的代码和你想做的事情,我建议你首先要确保你理解它们之间的区别
$(linkID).attr(...)
和$(“linkID”).attr(…)
它们是完全不同的东西,除非你清除它们,否则你将无法理解这是如何修复的。这里可能对你有点帮助
就守则而言,为了最简单的解决方法,请使用以下各项-
var linkID = this.id;
var theProp = $("#" + linkID).attr("href");
alert(linkID + "" + theProp);
或
我认为首先您需要了解jQuery库中的选择器。搜索ID时,应在选择器中使用#。但更容易使用此参数:$(this)