简体   繁体   English

如何从Kendo将参数传递给javascript函数<a href=“”></a>

[英]How to pass parameter to javascript function from kendo <a href=“”></a>

Here is the kendo grid I have <href> tag with onclick function().Now I want to pass filename as parameter to my onclick function.I want to pass that file name to the CallAjaxMethod Function as a parameter. 这是我具有onclick function()的<href>标记的kendo网格。现在我想将文件名作为参数传递给我的onclick函数。我想将该文件名作为参数传递给CallAjaxMethod函数。 How to do ths? 怎么办?

@(Html.Kendo().Grid<MVCAPPModels..AttachmentsModel>()
        .Name("grid")
        .Columns(columns =>
        {   
            columns.Bound(p => p.FileName).ClientTemplate("<a id='FileName' href='\\#' data-pdfurl='#= FileName #' onclick='CallAjaxMethod('#= FileName #');'>/#= FileName #</a>");

            columns.Bound(c => c.CreatedDate).Width(70);
            columns.Bound(c => c.CreatedBy).Width(70);
            columns.Bound(c => c.UpdatedDate).Width(70);
            columns.Bound(c => c.UpdatedbBy).Width(70);
        })

        .HtmlAttributes(new { style = "height: 350px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(1))
        .DataSource(dataSource => dataSource
            .Ajax()
                      .ServerOperation(false)

            .Read(read => read.Action("Customers_Read", "Home"))
        )
)

JavaScript function : JavaScript函数:

function CallAjaxMethod()
    {
        alert("page! CallAjax");
        $.ajax({
            type: "POST",
            url: "@Url.Action("Submit", "Home")",
            data: { fileName: "/pdfSample.pdf" },
            type: 'GET',
            cache: false,
            success: function (result) {
                        $('#partialView_div').data(result);
            }
        });
    }

First, add a parameter to your function, so you can actually use what is passed in: 首先,向函数添加参数,以便您可以实际使用传入的内容:

function CallAjaxMethod(filename)
{
    alert("page! CallAjax");
    $.ajax({
        type: "POST",
        url: "@Url.Action("Submit", "Home")",
        data: { fileName: filename },
        type: 'GET',
        cache: false,
        success: function (result) {
                    $('#partialView_div').data(result);
        }
    });
}

Then your HTML and Javascript quotes are conflicting with each other because they are all single quotes, so you need to change some of them, preferably the HTML ones: 然后您的HTML和Javascript引号相互冲突,因为它们都是单引号,因此您需要更改其中一些,最好是HTML引号:

@(Html.Kendo().Grid<MVCAPPModels.AttachmentsModel>()
        .Name("grid")
        .Columns(columns =>
        {   
            columns.Bound(p => p.FileName).ClientTemplate("<a id=\"FileName\" href=\"\\#\" data-pdfurl=\"#= FileName #\" onclick=\"CallAjaxMethod('#= FileName #');\">/#= FileName #</a>");

            columns.Bound(c => c.CreatedDate).Width(70);
            columns.Bound(c => c.CreatedBy).Width(70);
            columns.Bound(c => c.UpdatedDate).Width(70);
            columns.Bound(c => c.UpdatedbBy).Width(70);
        })

        .HtmlAttributes(new { style = "height: 350px;" })
        .Scrollable()
        .Groupable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(1))
        .DataSource(dataSource => dataSource
            .Ajax()
                      .ServerOperation(false)

            .Read(read => read.Action("Customers_Read", "Home"))
        )
)

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

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