简体   繁体   English

Javascript onclick函数错误无法读取未定义的属性“ inv”

[英]Javascript onclick function error Cannot read property 'inv' of undefined

I have javascript function as below: 我有如下JavaScript函数:

function myClickEvent(params) {
    var inv = $("#" + params["inv"]);
    inv.on("click", function () {
        $.ajax({
            url: params["route"],
            contentType: "text/json",
            success: function (result) {
                history.pushState(null, null, params["route"]);
                $("#content-wrapper").html(result);
                inv.parent().siblings().removeClass("active");
                inv.parent().addClass("active");
            }
        });
        return false;
    });
}

I am calling this into my html file like this: 我这样称呼它到我的html文件中:

<button onclick='myClickEvent()'>Click me</button>

It is showing error 显示错误

Cannot read property 'inv' of undefined 无法读取未定义的属性“ inv”

How to test this function? 如何测试此功能?

params is supposed to be a Javascript object with the inv property, which you have not provided as an argument to the function. params应该是具有inv属性的Javascript对象,您尚未将其作为函数的参数提供。

 function myClickEvent(params) { var inv = $("#" + params["inv"]); inv.on("click", function () { console.log(inv+": clicked"); //$.ajax({ // url: params["route"], //contentType: "text/json", //success: function (result) { // history.pushState(null, null, params["route"]); // $("#content-wrapper").html(result); // inv.parent().siblings().removeClass("active"); // inv.parent().addClass("active"); // } // }); // return false; }); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button onclick='myClickEvent({inv: "test"})'>Click me</button> <p/> <button id="test">Test</button> 

That's because you are not passing anything to the function, you can test it this way 那是因为您没有向函数传递任何东西,所以可以这样测试

<button onclick='myClickEvent("value")'>Click me</button>

and then add console.log(params) in the first line of your function 然后在函数的第一行中添加console.log(params)

您需要阅读一个JavaScript教程,您告诉按钮在不带参数的情况下单击函数myClickEvent()即可执行该按钮,并且在定义中您期望的是params param,这本身并没有填充,我会告诉您添加参数,但您真正需要做的是不使用html中的onClick属性,而是使用button元素中脚本中的addEventListener。

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

相关问题 无法读取 Javascript 函数中未定义的属性“样式” - Cannot read property 'style' of undefined in Javascript Function JavaScript无法读取未定义错误的属性“ classList” - JavaScript Cannot read property 'classList' of undefined Error 无法读取未定义错误 JavaScript 的属性“forEach” - Cannot read property 'forEach' of undefined error JavaScript 无法读取未定义的属性“长度” - JavaScript 中的错误 - Cannot read property 'length' of undefined - Error in JavaScript 创建了一个 onclick function 以从 ChartsJs 中的折线图中删除数据,但出现“无法读取未定义的属性‘数据’”错误 - Created an onclick function to remove data from a line chart in ChartsJs, but getting “Cannot read property 'data' of undefined” error React:传入onClick函数时无法读取undefined属性 - React: cannot read property of undefined when passing in my onClick function javascript错误“无法读取未定义的属性” - javascript error 'cannot read property of undefined' 无法读取javascript错误中未定义的属性“preventDefault” - Cannot read property 'preventDefault' of undefined in javascript error 具有JavaScript的CSS-无法读取属性“未定义”错误 - CSS with JavaScript - Cannot read property 'undefined' error JavaScript 错误:无法读取未定义的属性“推送” - JavaScript Error: Cannot read property 'push' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM