使用 jQuery 切换输入禁用属性
这是我的代码:
$("#product1 :checkbox").click(function(){
$(this)
.closest('tr') // Find the parent row.
.find(":input[type='text']") // Find text elements in that row.
.attr('disabled',false).toggleClass('disabled') // Enable them.
.end() // Go back to the row.
.siblings() // Get its siblings
.find(":input[type='text']") // Find text elements in those rows.
.attr('disabled',true).removeClass('disabled'); // Disable them.
});
如何切换 .attr('disabled',false);
?
我在谷歌上似乎找不到它。
Here is my code:
$("#product1 :checkbox").click(function(){
$(this)
.closest('tr') // Find the parent row.
.find(":input[type='text']") // Find text elements in that row.
.attr('disabled',false).toggleClass('disabled') // Enable them.
.end() // Go back to the row.
.siblings() // Get its siblings
.find(":input[type='text']") // Find text elements in those rows.
.attr('disabled',true).removeClass('disabled'); // Disable them.
});
How do I toggle .attr('disabled',false);
?
I can't seem to find it on Google.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
.prop()
方法获取匹配元素集中第一个元素的属性值。参数
name
(例如disabled
、checked
、selected
)任何可以为 true 或 falsevalue
,可以是:(空)
返回当前值。boolean
设置属性值。function
为每个匹配的元素调用,返回值用于设置属性。通过了两个论点;第一个参数是索引(数字,从0开始,每个找到的元素都会增加)。第二个参数是元素的当前值(true/false)。因此,在本例中,我使用了一个函数来为我提供索引 (
i
) 和当前值 (v
),然后返回当前值的相反值,因此财产状态被逆转。其他信息
.prop()
方法仅获取匹配集中的第一个 元素的属性值。对于尚未设置的属性值,或者匹配的集合没有元素,它返回 undefined。要单独获取每个元素的值,请使用循环构造,例如 jQuery 的.each()
或.map()
方法。属性与属性
属性和属性之间的区别在特定情况下可能很重要。在 jQuery 1.6 之前,
.attr()
方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。从 jQuery 1.6 开始,.prop()
方法提供了一种显式检索属性值的方法,而.attr()
则检索属性。The
.prop()
method gets the value of a property for the first element in the set of matched elements.Arguments
name
(e.g.disabled
,checked
,selected
) anything that can either be true or falsevalue
, can be:(empty)
returns the current value.boolean
sets the property value.function
Is called for each matched element, the returned value is used to set the property. There are two arguments passed; the first argument is the index (number, starting with 0, increases for each found element). The second argument is the current value of the element (true/false).So in this case, I used a function that supplied me the index (
i
) and the current value (v
), then I returned the opposite of the current value, so the property state is reversed.Additional information
The
.prop()
method gets the property value for only the first element in the matched set. It returns undefined for the value of a property that has not been set, or if the matched set has no elements. To get the value for each element individually, use a looping construct such as jQuery's.each()
or.map()
method.Attributes vs. Properties
The difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the
.attr()
method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the.prop()
method provides a way to explicitly retrieve property values, while.attr()
retrieves attributes.我猜测要获得完整的浏览器兼容性
disabled
应该通过值disabled
设置或删除!这是我刚刚制作的一个小插件:
示例链接。
编辑:更新了示例链接/代码以保持可链接性!
编辑2:
基于 @lonesomeday 评论,这是一个增强版本:
I guess to get full browser comparability
disabled
should set by the valuedisabled
or get removed!Here is a small plugin that I've just made:
Example link.
EDIT: updated the example link/code to maintaining chainability!
EDIT 2:
Based on @lonesomeday comment, here's an enhanced version:
另一个简单的选项,只需单击复选框即可更新。
HTML:
jQuery:
实际操作:链接
Another simple option that updates on a click of the checkbox.
HTML:
jQuery:
In action: link
过了一段时间,多亏了@arne,我创建了这个类似的小函数来处理输入应该被禁用和隐藏,或启用和显示的位置:
然后是一个 jQuery 对象(例如 $('input[name="something" ]') ) 只需使用以下命令即可切换:
Quite a while later, and thanks to @arne, I created this similar small function to handle where the input should be disabled AND hidden, or enabled AND shown:
Then a jQuery object (such as $('input[name="something"]') ) is simply switched using:
使用
attr
的回调语法,这相当简单:This is fairly simple with the callback syntax of
attr
: