1

我有以下。有什么方法可以将这两个事件结合起来吗?我需要某种 foreach 功能吗?

$('#AccountID').focus(function() {
    $('option[value="99"]', this).remove();
});

$('#TopicID').focus(function() {
    $('option[value="99"]', this).remove();
});​
4

3 回答 3

4

你只需要一个不同的选择器:

$('#AccountID, #TopicID').focus(...);

或者,更好的是,添加一个公共类并改用它。

于 2012-05-20T02:51:45.183 回答
1

这取决于组合事件的含义。你可以这样做:

$('#AccountID, #TopicID')
    .focus(function () {
                $('option[value="99"]', this).remove();
            });
于 2012-05-20T02:52:04.083 回答
1

jQuery 选择器使用 css 选择器,所以选择器的a,b 意思是,选择a b

$('#AccountID, #TopicID').focus(function () {
    $('option[value="99"]', this).remove();
});

请注意,当您为 jQuery 函数提供上下文时,它会以这种方式执行:

$(this).find('option[value="99"]').remove();

所以你可以自己做,节省(虽然很小)的旅行和开销。

jQuery 源代码:

...
...
...
// HANDLE: $(expr, context)
// (which is just equivalent to: $(context).find(expr)
} else {
    return this.constructor( context ).find( selector );
}
于 2012-05-20T02:52:38.660 回答