简体   繁体   English

通过单击动态插入的链接来调用函数

[英]call a function by clicking on a dynamically-inserted link

I have a MVC view that has a link in jquery, by clicking this link it calls a JavaScript function. 我有一个MVC视图,在jquery中有一个链接,通过单击此链接可以调用JavaScript函数。 I am not sure how to call the function and pass a parameter to the function. 我不确定如何调用该函数并将参数传递给该函数。

this is the link: 这是链接:

var cell5 = $('<td />').html('<a href="functioncall?FileName=' + 
e.Application_Filename + '&RemoteRefNumber=' + e.RemoteRefNumber + '&target="_blank">PDF</a>');

and this is the function I would like to call. 这是我想调用的函数。

 function getpdf(FileName,RemoteRefNumber) {
     [...]
 }

You can just wire this up inline via onclick , passing in your parameters. 您可以通过onclick将参数直接内联起来。 I assume you want to open a new browser window with some query params - observe the following... 假设您想使用一些查询参数打开一个新的浏览器窗口-请注意以下内容...

 var cell5 = $('<td />').html('<a href="#" onclick="getpdf(\'' + e.Application_Filename + '\', \''  + e.RemoteRefNumber + '\');">PDF</a>');

window.getpdf = function(FileName, RemoteRefNumber) {

    // do you need to do anything else here?
    window.open('https://[...]?FileName=' + FileName + '&RemoteRefNumber=' + RemoteRefNumber);
}

Depending on what you're doing, why not just href it? 根据您在做什么,为什么不只是href呢? Do you need to get into that function first? 您是否需要先进入该功能? If not, this should suffice... 如果没有,就足够了...

var cell5 = $('<td />').html('<a target="_blank" href="https://[...]?FileName=' + e.Application_Filename + '&RemoteRefNumber=' + e.RemoteRefNumber + '">PDF</a>')

note - in the first suggestion, I defined my function globally on window . 注意-在第一个建议中,我在window全局定义了我的函数。 Elsewise, that function is not available inline. 另外,该功能不能内联使用。 See this JSFiddle for a demonstration between three different function declarations and the behavior 请参阅此JSFiddle ,以了解三个不同的函数声明和行为之间的演示。

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

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