从我发现的所有示例中,如果选中复选框,则使用is(':checked')
应返回true,否则返回false。在我的情况下,它在两种情况下都返回false。我的错是什么?
{
xtype: 'checkbox',
boxLabel: 'Show message',
id: 'mainBox',
handler: function() {
alert($(this).is(':checked'));
}
答案 0 :(得分:3)
根据 ExtJS documentation ,我会尝试这样做:
{
xtype: 'checkbox',
boxLabel: 'Show message',
id: 'mainBox',
handler: function (field , value) {
alert(value);
}
}
答案 1 :(得分:1)
因为extjs事件处理程序中的this
未引用dom元素,而this
将引用extjs复选框对象。 jQuery将无法使用extjs对象引用找出目标元素。相反,您使用复选框的getValue()方法将返回已检查的状态值,因此
尝试
{
xtype: 'checkbox',
boxLabel: 'Show message',
id: 'mainBox',
handler: function (el) {
alert(el.getValue());
}
}
答案 2 :(得分:1)
正如您可以在jQuery文档中看到的那样,.is('...')函数返回一个jQuery对象。
如果您想要某种布尔值作为返回值,可以尝试以下代码:
alert($(this).is(':checked').length > 0);
但是对于复选框元素,使用'checked'属性也应该有效:
alert(this.checked);
如果你想使用jQuery,你甚至可以试试这个:
alert($(this).attr('checked') === 'checked');