[英]How do I call this JS function and pass a parameter through?
我的SignalR集线器具有以下脚本,并且在调用函数时遇到麻烦,因此无法传递参数。
$(function () {
var hub = $.connection.commentsHub;
$.connection.hub.start().done(function () {
function deleteComment(commentId) {
hub.server.DeleteComment(commentId);
}
});
});
然后在我的注释列表中,我试图以此调用我的deleteComment函数,但出现错误,指出未定义deleteComment。
<a onclick="deleteComment(@item.CommentId)">Delete</a>
如何调用我的deleteComment函数?
还是有更好的方法将参数传递给我的服务器?
您的deleteComment()
函数隐藏在其他函数的两层中,因此内联JavaScript无法访问它。 我建议(1)从done
回调中删除它,因为没有理由将其放在其中,以及(2)为此使用不引人注目的JavaScript:
HTML:
<a href="#" class="delete-comment" data-commentid="@item.CommentId">Delete</a>
JavaScript的:
$(function () {
var hub = $.connection.commentsHub;
function deleteComment(commentId) {
hub.server.DeleteComment(commentId);
}
$.connection.hub.start().done(function () {
$(".delete-comment").click(function() {
deleteComment($(this).attr("data-commentid"));
});
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.