具有 javascript onclick 功能的 CSHTML 按钮有时只能工作?

CSHTML button with javascript onlick function only works some times?

我在网页上设置了一个下载按钮,该按钮根据发布的问题数量迭代分配了一个 ID。

这是按钮:

<input data-bind="attr: { id: $index() }" type="button" value="Download" class="download" />

这是找到分配的号码并执行 onclick 的 JS 函数:

@Scripts.Render("~/bundles/knockout")

<script type="text/javascript">
var SDNo = 0;
    $(document).ready(function () {
            SystemJS.import('sd/questions').then(function (modules) {
                  //Code here for another section that fills out the questions not relevant apart from assigning SDNo

            });
    
            SystemJS.import('sd/download').then(function (modules2) {
                var attachVM = new modules2.Attachment();
                //$("#download").click(function () {
                $("input[class^='download'], input[class*=' download']").each(function () {
                    $(this).click(function () {
                        var id = $(this).attr('id');
                        let passedValue = id.concat("-" + SDNo);
                        attachVM.download(passedValue);
                    });
                });
            
    
            });

上述函数允许我转到打字稿文件并处理所需的 API 调用以获取文件

代码在这里:

打字稿

export class Attachment {

    async download(ID: Number) {
        window.open(await WebApi.getJSON('SD', 'Download', Number));

    }
}

所以是的,它会工作然后它会无缘无故地随机停止工作,我可以找到并且显然没有抛出错误,通过调试它甚至没有进入打字稿文件根本没有任何反应。但有时它会一直进入控制器做它需要做的事情。

根据@LouysPatriceBessette

$("input[class^='download'], input[class*=' download']").each(function () {
    $(this).click(function () {
    var id = $(this).attr('id');
    let passedValue = id.concat("-" + SDNo);
    attachVM.download(passedValue);
    });

$("input.download").on("click", function() {
    var id = $(this).attr('id');
    let passedValue = id.concat("-" + SDNo);
    attachVM.download(passedValue);
    });

它现在一直有效,再次感谢您。