gpt4 book ai didi

javascript - jQuery 中的启动/更改具有相同的单选按钮设置功能

转载 作者:行者123 更新时间:2023-11-28 18:56:41 25 4
gpt4 key购买 nike

我有一个共享 jQuery 函数,用于检查 RadioButton 选择:如果选择 1,则隐藏跨度,否则显示跨度。

这个共享函数在启动时和更改时都会被调用,因为在启动时,它需要做同样的事情。启动有效,但 onChange 引用不起作用:

JS_OBJ = {

toggleTier : function() {

if ($('input[name="tier"]:checked').val() == 'Y_YES')
{
$('#tierSpan').hide();
}
else
{
$('#tierSpan').show();
}

},

// this is called from document.onReady - it comes here, OK
onReady : function() {

// on startup, toggle Tier - works OK
this.toggleTier();

// Also link the radio button Change to this shared function
$('input[name="tier"]:radio').change(function () {
alert('About to enter toggle...');
// NEVER COMES HERE - Object doesn't support this property or method
this.toggleTier();
});

}

};

最佳答案

this 在穿过不同区域时会改变值。当它第一次实例化时,它有一个很好的值,但是 radiobutton:change 有一个不同的 this

我能够更改它使其正常工作:

    $('input[name="tier"]:radio').change(function () {
alert('About to enter toggle...');
self; //closure
toggleTier();
});

看到这个:What underlies this JavaScript idiom: var self = this?

关于javascript - jQuery 中的启动/更改具有相同的单选按钮设置功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33507777/

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