JQuery如何选择所有没有禁用并且没有只读属性的元素?

17
使用JQuery,我需要选择满足以下条件的所有输入元素:

'未被禁用' (:disabled) '并且' '非只读状态',

然后我将改变CSS样式以查询结果。
更新:我需要按顺序迭代结果。
2个回答

46

冗长地说:

$('input:not(:disabled):not([readonly])').each(function() {
     $(this).foo();
});

或者更好的方式:

$('input:enabled:not([readonly])').each(function() {
     $(this).foo();
});

编辑:根据您下面的答案,有一种更好的方法可以实现您想要做的事情:

$('input').focus(function(e) {
    if($(this).is(':disabled, [readonly]')) {
        $(this).next().focus();
        e.preventDefault();
    }
});

0

我真的需要这些:

$('input:not(input:enabled:not([readonly]))').each(function() {
 //skip focus to readonly input
    $(this).focus(function(){
        $(this).nextAll('input:enabled:not([readonly])')[0].focus();
    });
});

1
你应该看一下我的更新答案。有一种更简洁的方法来完成这个任务。 - Eric

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接