gpt4 book ai didi

jquery - 如何使用下划线_.wrap方法修改jquery select2方法($.fn.select2)

转载 作者:行者123 更新时间:2023-12-01 00:22:32 25 4
gpt4 key购买 nike

我有一个 select2 组合框的以下 HTML 代码 ( pnlkr link )

<label id="label-select2Home">My Home State: </label>
<div class="select2-home" xe-field="select2Home"></div>

JS 初始化:

$( ".select2-home" ).select2({
tokenSeparators: [",", " "],
allowClear: true,
placeholder: "Select a state",
data: stateData
});

我必须做什么:

我必须找到附近的标签,并将文本更改为“TEST LABEL”。

我正在尝试使用

执行上述操作
$.fn.select2 = _.wrap($.fn.select2, function changeLabel(org) {
// Replace the nearby label text with "TEST LABEL"
});

我无法做到

 // Replace the nearby label text with "TEST LABEL"
var args = Array.prototype.slice.call(arguments, 1); // use Array.slice function to copy the arguments array from 1-end, without 'org'
return org.apply( this, args );

它给了我错误,如何调用 .apply 方法来操作 select2 并更改标签文本。

请参阅plnkr对于上面的例子

最佳答案

问题在于 _.wrap 函数略有损坏,因为它完全清除了 $.fn.select2 的旧值。这样做时,它会清除 $.fn.select2.defaults,从而弄乱插件。

为了解决这个问题,您可以执行以下操作:

$.fn.select2 = _.wrap($.fn.select2, function changeLabel(org) {
// Replace the nearby label text with "TEST LABEL"

$.fn.select2.defaults = org.defaults;
var args = Array.prototype.slice.call(arguments, 1);
return org.apply(this, args);
});

关于jquery - 如何使用下划线_.wrap方法修改jquery select2方法($.fn.select2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27478787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com