使用单选按钮禁用所有表单元素

小田良

试图找到一种解决方案,如果第一个单选按钮(反馈)更改为“开”,但页面必须加载默认为“关”的反馈,则允许我启用表单中的所有字段。所有字段都必须在禁用状态下加载。如果反馈更改为“开”,则其他字段应启用。当然,如果选择了关闭,这些字段将再次被禁用。

我尝试了很多零碎的代码,试图拼凑一个单一的解决方案,但我无法弄清楚。许多解决方案都基于非常旧版本的 jQuery,而我使用的是当前版本。并不是说 jQuery 是必需的,纯 JavaScript 就可以了(如果可能的话)。

感谢任何帮助。这是代码。

<html>
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>

<label>Feedback</label>
<input id="controlon" class="w3-radio" type="radio" name="feedback" value="on">
<label>On</label>
<input id="controloff" class="w3-radio" type="radio" name="feedback" value="off" checked>
<label>Off</label>

<hr />

<label>Name</label>
<input id="name" class="" type="text" name="text">

<label>Species</label>
<select id="species" class="" name="species">
    <option value="0" disabled selected>- Select -</option>
    <option value="1">Cat</option>
    <option value="1">Dog</option>
</select>

<label>Age</label>
<input id="kp" class="" type="radio" name="age" value="on">
<label>Kitten/Puppy</label>
<input id="adult" class="" type="radio" name="age" value="off" checked>
<label>Adult</label>

<label>Comments</label>
<textarea id="comments" class="" rows="4"></textarea>

<button id="send" class="">Send</button>

</body>
</html>
小田良

这就是最终对我有用的东西

<html>
<head>
    <title></title>
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>

<div class="w3-container">
    <label>Feedback</label>
    <input id="controlon" class="w3-radio" type="radio" name="feedback" value="on">
    <label>On</label>
    <input id="controloff" class="w3-radio" type="radio" name="feedback" value="off" checked>
    <label>Off</label>
</div>

<hr />

<div class="w3-container">
    <label>Name</label>
    <input id="name" class="" type="text" name="text" disabled>

    <label>Species</label>
    <select id="species" class="" name="species" disabled>
        <option value="0" disabled selected>- Select -</option>
        <option value="1">Cat</option>
        <option value="1">Dog</option>
    </select>

    <label>Age</label>
    <input id="kp" class="" type="radio" name="age" value="on" disabled checked>
    <label>Kitten/Puppy</label>
    <input id="adult" class="" type="radio" name="age" value="off" disabled>
    <label>Adult</label>

    <label>Comments</label>
    <textarea id="comments" class="" rows="4" disabled></textarea>

    <button id="send" class="" disabled>Send</button>
</div>

<script>
    $('input:radio[name="feedback"]').change(function() {
        if ($(this).val()=='on') {
            $('#name,#species,#kp,#adult,#comments,#send').attr('disabled', false);
        } 
        else if ($(this).val()=='off') {
            $('#name,#species,#kp,#adult,#comments,#send').attr('disabled', true);
        }
    });
</script>

</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jQuery获取所有输入表单元素,包括单选

禁用div中的所有表单元素

纯JavaScript禁用div中的所有表单元素

禁用除特定类之外的所有表单元素

如何根据其他组件单选按钮选项禁用表单元素

如何使用单独的单选按钮表单元素正确映射 React 元素?

表单元素数组-分组单选按钮

Cakephp 3.0表单元素单选按钮选择问题

使用jQuery获取所有表单元素值?

禁用所有具有相同名称的单选按钮

单击n次后如何禁用所有单选按钮?

使用jQuery禁用除表单中的某些元素以外的所有输入元素

Bootstrap 表单验证禁用我的单选按钮

仅使用Javascript,如何防止输出禁用的表单元素?

在gridview上检查所有单选列表单选按钮是否已选中

有条件地在Angular 4的Reactive表单内的单选按钮组中禁用单选按钮

选择所有选定的表单元素以及不可选择的表单元素

使用下面的源代码删除单选按钮。我已经尝试过这段代码,除单选按钮外,所有元素都被删除

合适的js删除表格中的所有表单元素

多步表单按钮禁用,直到表单选择字段

在每个按钮单击时添加表单元素并使用 react

单选按钮组验证。使用警报Reactjs突出显示所有未选中的单选按钮问题

使用jQuery捕获div中的所有单选按钮

使用javascript选择所有选项A单选按钮

单击单选按钮时禁用或启用文本输入表单,使用 Jquery

在JQuery中使用“最近”禁用单选按钮

使用js启用和禁用单选按钮

使用jQuery启用禁用的单选按钮列表

如何使用jQuery禁用表单中的所有<input>?