通过单击字段启用非活动字段

2024-05-09

是否可以有一组非活动字段,如果单击其中一个字段,则某些字段将变为必填字段并运行某些代码段?举例来说,您显示了三个字段:

<input type="text" id="gov1" name="gov1">

<input type="text" id="parentb" name="parentb">

<input type="checkbox" name="child" id="child">

所有这三个字段最初都是不活动的;但是假设您单击其中一个字段,它现在会使其余字段处于活动状态和强制状态,并且某些代码段在“parentb”字段上运行,并且其属性为只读。

window.onload = function(event)
{
    var $input2 = document.getElementById('dec');
    var $input1 = document.getElementById('parentb');
    $input1.addEventListener('keyup', function()
    {
        $input2.value = $input1.value;
    });
}

我已经做了一些搜索,但我似乎找不到任何对这种特定情况有用的东西,而且我对 JavaScript 很陌生,所以任何形式的帮助都会很棒。使用 JavaScript 可以实现这一点吗?或者类似的事情可能吗?


您正在寻找的这种方法称为“链式选择”方法。

使用 jQuery 框架的例子如下:

<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<form action="" method="get">
    <label>
    <input name="Government" type="checkbox" value="">Government</label>
    <br>
    <label>
    <input name="Parent" type="checkbox" id="parent_child_group" value="">Parent</label>
    <br>
    <label>Name of Child
    <input type="text" name="parent_child" value="" class="parent_child_group"></label>
    <br>
    <label>Other
    <input type="text" name="parent_other" value="" class="parent_child_group"></label>
</form>

<script type="text/javascript">
    $(document).ready(function () {
         $(function() {
             enable_cb();
             $("#parent_child_group").click(enable_cb);
         });

         function enable_cb() {
             if (this.checked) {
                 $("input.parent_child_group").removeAttr("disabled");
             } else {
                 $("input.parent_child_group").attr("disabled", true);
             }
         }
    });        
    </script>
    </body>
    </html>

JSFiddle 上的工作示例:http://jsfiddle.net/farondomenic/fg7p8/1/ http://jsfiddle.net/farondomenic/fg7p8/1/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过单击字段启用非活动字段 的相关文章

随机推荐