启用/禁用带有复选框的输入字段(javascript) [英] enable/disable input fields with checkboxes (javascript)

查看:101
本文介绍了启用/禁用带有复选框的输入字段(javascript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用不同的输入字段制作一个html5表单,并带有一个可以启用/禁用输入字段的复选框。

I'm trying to make a html5 form with different input fields, with a checkbox that can enable/disable input fields.

选中该复选框时,它会应该可以在输入字段中输入数据。如果未选中此复选框,则应该无法在输入字段中输入数据。

When the checkbox is selected, it should be possible to enter data into an input field. When the checkbox isn't selected, it shouldn't be possible to enter data to the input field.

我已经尝试过了(只是尝试启用/禁用工作原理):

I already tried this (just to try if enabling/disabling works):

<script>
document.getElementById("checkbox").onclick = function() {
    document.getElementById("inputfield").disabled=true;
}
</script>

当用户单击复选框时,输入字段被禁用,因此禁用字段有效,但是我不不知道如何选择复选框并重新启用。

The input field is disabled when the user clicks on the checkbox, so disabling fields works but I don't know how it works with selecting the checkbox and re-enabling.

这是我第一次使用javascript,因此非常感谢您的帮助!

This is my first time using javascript so any help is greatly appreciated!

推荐答案

您检查复选框的状态

document.getElementById("inputfield").disabled= this.checked;

这篇关于启用/禁用带有复选框的输入字段(javascript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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