简体   繁体   English

'this'对象在jquery中具有参数的回调内部未定义

[英]'this' object is undefined inside callback having parameter, in jquery

I am attaching a callback function to select box on change event like this 我附加一个回调函数来选择这样的更改事件框

select.on('change', config, party.selectOne);

Here config is the parameter I am passing to call back function. 这里config是我传递给回调函数的参数。 Inside call back I could access config parameter. 在回调内部我可以访问配置参数。

But my issue is, inside callback, $(this) returns unexpected object other than select box object 但我的问题是,在回调内,$(this)返回选择框对象以外的意外对象

party.selectOne= function(config) {
    // is the selectbox
    var selectbox = $(this);
    var api = selectbox.data('api');
}

So I am getting api as undefined . 所以我得到的api未定义的 But this will work fine if I don't pass any parameter to call back. 但是如果我没有传递任何参数来回调,这将正常工作。 What is wrong with me? 我是怎么了?

Example : 示例:

<select data-api="{'name':'one','address':'address1'}">
    <option>one</option>
    <option>two</option>
</select>

So I am getting api as undefined. 所以我得到的api是未定义的。

That means that these two things are true: 这意味着这两件事情都是真的:

  1. The element the change occurred on (one of the elements in select as of your select.on call) has never had .data('api', something) called on it, and 发生change的元素( select.on调用中select中的一个元素)从未调用过.data('api', something) ,并且

  2. That element doesn't have a data-api attribute on it — or rather, it didn't as of the first time .data was used on it. 该元素没有data-api属性 - 或者更确切地说,它不是一次使用.data

Or , it could mean that party.selectOne has been bound at some point in code you haven't shown. 或者 ,这可能意味着party.selectOne已经绑定在您未显示的代码中的某个位置。

(Remember that data is not an accessor for data-* attributes. It's both more and less than that .) (请记住, data 不是 data-*属性的访问者。 它的数量越来越少 。)

jQuery's set-based nature can trip you up. jQuery的基于集合的性质可以让你失望。 If you have multiple elements in select as of your select.on call, and you've only set the api data item (directly via .data , or indirectly through a data-api attribute) on one of them, then a change event on a different one of them may give undefined for .data('api') . 如果select.on调用中有select中的多个元素,并且您只在其中一个上设置了api数据项(直接通过.data或间接通过data-api属性),那么就会发生change事件它们的不同的一个可以给undefined.data('api') jQuery's data cache is per-element, not per-set. jQuery的数据缓存是每个元素,而不是每个元素。

Not this is undefined, but the return value of data() for "api" is. this不是未定义的,但“api”的data()的返回值是。
Use attr() instead of data() : 使用attr()而不是data()

 // To make this example work var party = {}; var config = { foo: "bar" }; var select = $("select"); // --- party.selectOne = function(config) { var selectbox = $(this); var api = selectbox.attr('data-api'); console.log(api); console.log(config.data.foo); } select.on('change', config, party.selectOne); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <select data-api="{'name':'one','address':'address1'}"> <option>one</option> <option>two</option> </select> 

It is resolved. 它已经解决了。 Here config parameter was of type Object. 这里的config参数是Object类型。 If we pass config parameter as string, it will be resolved. 如果我们将config参数作为字符串传递,它将被解析。 I think, if we pass config as object , $(this) will refer config , instead of referencing select box element 我想,如果我们将config作为对象传递,$(this)将引用config ,而不是引用select box元素

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

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