在jQuery中基于Checkbox切换输入上的禁用属性 [英] Toggle Disabled Attribute on Input Based on Checkbox in jQuery

查看:95
本文介绍了在jQuery中基于Checkbox切换输入上的禁用属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个默认未选中的复选框,默认情况下禁用输入。

I have a checkbox that is unchecked by default and a disabled input by default.

<label class="checkbox span3"><input type="checkbox"> I am a full-time student.</label>
<input class="inputIcon span3" id="disabledInput" type="text" placeholder="Enter School Name" disabled>

我可以完全控制类和id名称,并且该站点使用jQuery,因此可以使用或简单如果需要,则使用javascript。

I have full control over the class and id names and the site uses jQuery so can use that or plain javascript if needed.

如果用户选中此框,则应从以下输入中删除已禁用属性。如果用户取消选中,它将再次被禁用。

If a user checks the box, then the "disabled" attribute should be removed from the following input. If the user unchecks it should become disabled again.

在StackOverflow上发现了几个类似的问题,但似乎没有这个确切的用例。

Found a several similar questions on StackOverflow but none seem to be this exact use case.

推荐答案

您必须将ID分配给复选框以将点击绑定到特定复选框,

You have to assign id to checkbox to bind the click to particular checkbox,

现场演示

Live Demo

<input type="checkbox" id="chk">

$("#chk").click(function(){   
    $("#disabledInput").attr('disabled', !this.checked)
});

这篇关于在jQuery中基于Checkbox切换输入上的禁用属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆