Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在通过javascript选中特定复选框时启用禁用文本框_Javascript_Asp.net - Fatal编程技术网

如何在通过javascript选中特定复选框时启用禁用文本框

如何在通过javascript选中特定复选框时启用禁用文本框,javascript,asp.net,Javascript,Asp.net,对于本页: <input type="checkbox" checked id="chk1" onclick="myFunction(this)" /> <input type="checkbox" checked id="chk2" onclick="myFunction(this)" /> <input type="checkbox" checked id="chk3" onclick="myFunction(this)" /> <input typ

对于本页:

<input type="checkbox" checked id="chk1" onclick="myFunction(this)" />
<input type="checkbox" checked id="chk2" onclick="myFunction(this)" />
<input type="checkbox" checked id="chk3" onclick="myFunction(this)" />
<input type="checkbox" checked id="chk4" onclick="myFunction(this)" />

<input type="text" id="txt1" disabled="disabled" />
<input type="text" id="txt2" disabled="disabled" />
<input type="text" id="txt3" disabled="disabled" />
<input type="text" id="txt4" disabled="disabled" />

<script>
    function myFunction(el) {
        var txt = document.getElementById(el.id.replace('chk', 'txt'));

        if (el.checked) {
            document.getElementById(txt).removeAttribute("disabled");
        }
        else
            document.getElementById(txt).setAttribute("disabled", "disabled");
    }
</script>
我已经尝试过这个脚本,但它给出的错误如下

Microsoft JScript运行时错误:“document.getElementById…”为 空或不是对象


您的脚本部分有一个错误:

var txt = document.getElementById(el.id.replace('chk', 'txt'));
这将返回文本框的整个元素,而不是id。如果更改为此项,它应该可以工作:

<script>
    function myFunction(el) {
        var txt = el.id.replace('chk', 'txt');

        if (el.checked) {
            document.getElementById(txt).removeAttribute("disabled");
        }
        else
            document.getElementById(txt).setAttribute("disabled", "disabled");
    }
</script>
您可以在这个do note中测试它,我还删除了fiddle示例中复选框的checked属性,并使用var txt=el.id.replace'chk',txt'

而不是 var txt=document.getElementByIdel.id.replace'chk','txt'

无需使用document.getElementById,因为您已经在myFunctionthis中使用元素进行声明

并使用true | false禁用应用

函数myFunctionel{ var txt=el.id.replace'chk','txt'; 如果el检查{ document.getElementByIdtxt.disabled=false; } 其他的 document.getElementByIdtxt.disabled=true; }
使用此语句,您将创建文本框

var txt = document.getElementById(el.id.replace('chk', 'txt'))
但是你不应该用这个

document.getElementById(txt).removeAttribute("disabled");
但是这个

txt.removeAttribute("disabled");
比如看小提琴


。如果它仍然不适用于yo,只需将脚本移动到html标记上方即可。

TextBox不能使用此脚本启用
function myFunction(el) {
    var txt = document.getElementById(el.id.replace('chk', 'txt'));

    if (el.checked) {
        txt.removeAttribute("disabled");
    }
    else
        txt.setAttribute("disabled", "disabled");
}