选中和取消选中复选框上的启用和禁用文本输入 [英] enabled and disabled text input on checkbox checked and unchecked

查看:116
本文介绍了选中和取消选中复选框上的启用和禁用文本输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有复选框和文字输入

当我选中复选框时,我需要启用文本输入,当我取消选中复选框时禁用文本输入

What I need is to enable text input when i check the checkbox and to disable text input when i uncheck the checkbox

我正在使用以下代码,但在检查时取消选中/禁用时会执行反向启用,以便如何根据我的需要进行调整。

I'm using the following code but it do the reverse enable when unchecked / disable when checked so how to adjust it fit with my needs.

<input type="checkbox" id="yourBox">
<input type="text" id="yourText">
<script>
document.getElementById('yourBox').onchange = function() {
document.getElementById('yourText').enabled = this.checked;
};
</script>

任何帮助〜谢谢

推荐答案

您只需要在 this.checked 前添加

这是一个显示变化的例子:

Here's an example that shows the change:

document.getElementById('yourBox').onchange = function() {
    document.getElementById('yourText').disabled = !this.checked;
};

<input type="text" id="yourText" disabled />
<input type="checkbox" id="yourBox" />

这篇关于选中和取消选中复选框上的启用和禁用文本输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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