简体   繁体   English

未定义onclick调用的方法

[英]onclick called method is not defined

I have the code running on jsfiddle . 我的代码在jsfiddle上运行。 The code is below. 代码如下。 I can bet a ton of money this is me doing something stupid, but I can't find it. 我敢打赌,这是我在做一些愚蠢的事情,但我找不到。

Clicking on says that Uncaught ReferenceError: showDetail is not defined 单击表示未定义的ReferenceError:showDetail未定义

I have tried varying adding of parameters as well just to try and troubleshoot. 我尝试过各种添加参数的方法,只是为了进行故障排除。 Passing none, one, two doesn't effect the error message. 不传递,传递1、2不会影响错误消息。

const lcAppdata = {

    matters: [{
        mid: 100,
        title: "Matter1",
        dateOpened: "01/01/17",
        decedent: "Elvis Presley",
        personalRepresentative: "Alexis Texas",
        attorneyAccountInfo: "John Holmes"
    }, {
        mid: 200,
        title: "Matter2",
        dateOpened: "01/02/16",
        decedent: "Kurt Cobain",
        personalRepresentative: "Kagney Linn Karter",
        attorneyAccountInfo: "Simon Rex"
    }, {
        mid: 300,
        title: "Matter3",
        dateOpened: "01/03/15",
        decedent: "Bob Marley",
        personalRepresentative: "Nikki Benz",
        attorneyAccountInfo: "Ron Jeremy"
    }],
    currentMatter: ''
};


function showDetail(mid){
    console.log(mid);
};

function init(){
    for(let matter of lcAppdata.matters){
    $('#tbodyMatterList').append(`<tr 
            onclick="showDetail(${matter.mid})"> 
            <th scope="row" >${matter.title}</th>
            <td bind>${matter.decedent}</td>
            <td bind>${matter.dateOpened}</td>
            <td bind>${matter.personalRepresentative}</td>
            <td bind>${matter.attorneyAccountInfo}</td></tr>`);

    }


};

init();

You code is fine, it's because of how jsfiddle runs your script. 您的代码很好,这是因为jsfiddle运行脚本的方式。 It's set to run the script onLoad ie all your code is actually run inside the callback of load event so your function showDetail is not actually defined as a global function, but only local to the load event callback 它设置为运行脚本onLoad即所有代码实际上都在load事件的回调内部运行,因此您的showDetail函数实际上并未定义为全局函数,而仅在load事件回调本地

You can either change your code to attach the showDetail function to the window object to make it global like this 您可以更改代码以将showDetail函数附加到window对象以使其像这样全局

function showDetail() {
  console.log($(this).attr('data-mid'));
};

window.showDetail = showDetail;

or change jsfiddle settings to run your script globally like this 或更改jsfiddle设置以像这样全局运行脚本

在此处输入图片说明

You can do it in the following way 您可以通过以下方式进行

function showDetail() {
  console.log($(this).attr('data-mid'));
};


(function init() {
    for (let matter of lcAppdata.matters) {
        $('#tbodyMatterList').append(`<tr data-mid=${matter.mid}><th scope="row" >${matter.title}</th>
        <td bind>${matter.decedent}</td>
        <td bind>${matter.dateOpened}</td>
        <td bind>${matter.personalRepresentative}</td>
        <td bind>${matter.attorneyAccountInfo}</td></tr>`);
    }

    $('#tbodyMatterList tr').click(showDetail);
})();

In JSFiddle you have to init methods in a different way the way you want to achieve it 在JSFiddle中,您必须以不同的方式初始化方法来实现它

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

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