Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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_Html - Fatal编程技术网

Javascript-基于下拉列表启用/禁用元素和文本框不工作

Javascript-基于下拉列表启用/禁用元素和文本框不工作,javascript,html,Javascript,Html,这是从一本书中摘取的。但是我想在从下拉列表中选择值“Others”时启用文本框 这是我从JSFIDLE编辑的代码 <html> <head> <title> Submit a Contract </title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script typ

这是从一本书中摘取的。但是我想在从下拉列表中选择值“Others”时启用文本框

这是我从JSFIDLE编辑的代码

<html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">
function toggleExternal() {
    document.getElementById("extText").disabled = false;

    var divis_el = document.getElementById("division");
    for (var i = 0; i < divis_el.children.length; i++) {
        divis_el.children[i].disabled = true;
    }
}
function toggleDivision() {
    document.getElementById("extText").disabled = true;z
    var divis_el = document.getElementById("division");
    for (var i = 0; i < divis_el.children.length; i++) {
        divis_el.children[i].disabled = false;
    }
}

function enableTextbox() {
var val = document.getElementById("mySelect").selectedIndex;
if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4 ||) { document.getElementById("otherTxt").disabled = true}
if (val == 5) { document.getElementById("otherTxt").disabled = true; }
}
</script>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    ID:<br>
    <input type="hidden" name="id" value="50" />

    <label for = "client1">
    <input type="radio" name="client_type" id = "client1" value="Division" checked onclick="toggleDivision()"/> Division
    </label>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
    <label for ="client2">
    <input type="radio" name="client_type" id = "client2" value="External"  onclick="toggleExternal()"/> External
    </label>
    &nbsp 
    <input disabled type="text" id="extText" name="client_details2" value=""/> 
    <br><br>

    <div id="division">
    Division:
    <select id="mySelect" name="client_details" onchange="enableTextbox()">
        <option value="Choose">Choose Division...</option>
        <option value="Distribution">Distribution</option>
        <option value="Transmission">Transmission</option>
        <option value="Generation">Generation</option>
        <option value="Procument">Procument</option>
        <option value="Other">Others</option>
    </select>   
    <br><br>
    Others:<input type="text" id="otherTxt" name="client_details1" value="" disabled/>
    <br>
    </div>
    <br>
    <input type="submit" name="submit" value="Submit"/>
</form>     
</body>

提交合同
函数toggleExternal(){
document.getElementById(“extText”).disabled=false;
var divis_el=document.getElementById(“division”);
for(var i=0;i
分部
        
外部的
 


分部: 选择部门。。。 分布 传输 一代 采购 其他

其他:

我添加了函数enableTextBox,然后禁用/启用的元素突然不起作用。为什么呢

下面是工作代码

    <html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    ID:<br>
    <input type="hidden" name="id" value="50" />

    <label for = "client1">
    <input type="radio" name="client_type" id = "client1" value="Division" checked onclick="toggleDivision()"/> Division
    </label>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
    <label for ="client2">
    <input type="radio" name="client_type" id = "client2" value="External"  onclick="toggleExternal()"/> External
    </label>
    &nbsp 
    <input disabled type="text" id="extText" name="client_details2" value=""/> 
    <br><br>

    <div id="division">
    Division:
    <select id="mySelect" name="client_details" onchange="enableTextbox()">
        <option value="Choose">Choose Division...</option>
        <option value="Distribution">Distribution</option>
        <option value="Transmission">Transmission</option>
        <option value="Generation">Generation</option>
        <option value="Procument">Procument</option>
        <option value="Other">Others</option>
    </select>   
    <br><br>
    Others:<input type="text" id="otherTxt" name="client_details1" value="" disabled/>
    <br>
    </div>
    <br>
    <input type="submit" name="submit" value="Submit"/>
</form>  
<script>
    function toggleExternal() {
        document.getElementById("extText").disabled = false;

        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = true;
        }
    }
    function toggleDivision() {
        alert('a')
        document.getElementById("extText").disabled = true;
        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = false;
        }
    }

    function enableTextbox() {
    var val = document.getElementById("mySelect").selectedIndex;
    if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4) { document.getElementById("otherTxt").disabled = true}
    if (val == 5) { document.getElementById("otherTxt").disabled = false; }
    }
</script>   
</body>

提交合同
ID:
分部          外部的  

分部: 选择部门。。。 分布 传输 一代 采购 其他

其他:

函数toggleExternal(){ document.getElementById(“extText”).disabled=false; var divis_el=document.getElementById(“division”); for(var i=0;i
这是工作代码

    <html>
<head>
<title> Submit a Contract </title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    ID:<br>
    <input type="hidden" name="id" value="50" />

    <label for = "client1">
    <input type="radio" name="client_type" id = "client1" value="Division" checked onclick="toggleDivision()"/> Division
    </label>
    &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
    <label for ="client2">
    <input type="radio" name="client_type" id = "client2" value="External"  onclick="toggleExternal()"/> External
    </label>
    &nbsp 
    <input disabled type="text" id="extText" name="client_details2" value=""/> 
    <br><br>

    <div id="division">
    Division:
    <select id="mySelect" name="client_details" onchange="enableTextbox()">
        <option value="Choose">Choose Division...</option>
        <option value="Distribution">Distribution</option>
        <option value="Transmission">Transmission</option>
        <option value="Generation">Generation</option>
        <option value="Procument">Procument</option>
        <option value="Other">Others</option>
    </select>   
    <br><br>
    Others:<input type="text" id="otherTxt" name="client_details1" value="" disabled/>
    <br>
    </div>
    <br>
    <input type="submit" name="submit" value="Submit"/>
</form>  
<script>
    function toggleExternal() {
        document.getElementById("extText").disabled = false;

        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = true;
        }
    }
    function toggleDivision() {
        alert('a')
        document.getElementById("extText").disabled = true;
        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = false;
        }
    }

    function enableTextbox() {
    var val = document.getElementById("mySelect").selectedIndex;
    if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4) { document.getElementById("otherTxt").disabled = true}
    if (val == 5) { document.getElementById("otherTxt").disabled = false; }
    }
</script>   
</body>

提交合同
ID:
分部          外部的  

分部: 选择部门。。。 分布 传输 一代 采购 其他

其他:

函数toggleExternal(){ document.getElementById(“extText”).disabled=false; var divis_el=document.getElementById(“division”); for(var i=0;i
您在这一行中犯了1个错误。 在
val==4
之后,您输入了“或”符号“|”,这在JS中是不允许的

第二个错误是“otherTxt”字段已禁用,您正在尝试禁用它。因此,请更改值“disable=false


这一行你犯了一个错误。 在
val==4
之后,您输入了“或”符号“|”,这在JS中是不允许的

第二个错误是“otherTxt”字段已禁用,您正在尝试禁用它。因此,请更改值“disable=false


如果您可以在单击“外部”时选择“其他”菜单,然后再次单击“分区”字段,您将在此处启用“其他”文本框。所以这个问题也固定在这里。像这样更改if条件。检查问题并解决它。这将帮助您更好地为您服务,而不会出现问题

   <html>
    <head>
    <title> Submit a Contract </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript">
    function toggleExternal() {
        document.getElementById("extText").disabled = false;

        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = true;
        }

    }
    function toggleDivision() {
        document.getElementById("extText").disabled = true; 
        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = false;
        }
        var val = document.getElementById("mySelect").selectedIndex;
    if (val != 5 ) { document.getElementById("otherTxt").disabled = false}else{document.getElementById("otherTxt").disabled = true; }

    }

    function enableTextbox() {
    var val = document.getElementById("mySelect").selectedIndex;
    if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4 && val != 5 ) { document.getElementById("otherTxt").disabled = false}else{document.getElementById("otherTxt").disabled = true; }

    }
    </script>
    </head>
    <body>
    <form method="post" action="" enctype="multipart/form-data">
        ID:<br>
        <input type="hidden" name="id" value="50" />

        <label for = "client1">
        <input type="radio" name="client_type" id = "client1" value="Division" checked onclick="toggleDivision()"/> Division
        </label>
        &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
        <label for ="client2">
        <input type="radio" name="client_type" id = "client2" value="External"  onclick="toggleExternal()"/> External
        </label>
        &nbsp 
        <input disabled type="text" id="extText" name="client_details2" value=""/> 
        <br><br>

        <div id="division">
        Division:
        <select id="mySelect" name="client_details" onchange="enableTextbox()">
            <option value="Choose">Choose Division...</option>
            <option value="Distribution">Distribution</option>
            <option value="Transmission">Transmission</option>
            <option value="Generation">Generation</option>
            <option value="Procument">Procument</option>
            <option value="Other">Others</option>
        </select>   
        <br><br>
        Others:<input type="text" id="otherTxt" name="client_details1" value="" />
        <br>
        </div>
        <br>
        <input type="submit" name="submit" value="Submit"/>
    </form>     
    </body>

提交合同
函数toggleExternal(){
document.getElementById(“extText”).disabled=false;
var divis_el=document.getElementById(“division”);
for(var i=0;i<html>
    <head>
    <title> Submit a Contract </title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="text/javascript">

        function clientDetails(){

            console.log(document.getElementById("client_details").value);
            if(document.getElementById("client_details").value == "Other"){
        document.getElementById("client_details1").disabled = false;
      }else{
        document.getElementById("client_details1").disabled = true;
      }
    }

    function toggleExternal() {
        document.getElementById("extText").disabled = false;
        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = true;
        }
    }
    function toggleDivision() {
        document.getElementById("extText").disabled = true;
        var divis_el = document.getElementById("division");
        for (var i = 0; i < divis_el.children.length; i++) {
            divis_el.children[i].disabled = false;
        }
}
</script>
</head>
<body>
    <form method="post" action="" enctype="multipart/form-data">
        ID: 50<br>
        <input type="hidden" name="id" value="50" />

        <label for = "client1">
        <input type="radio" name="client_type" id = "client1" value="Division" checked onclick="toggleDivision()"/> Division
        </label>
        &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp 
        <label for ="client2">
        <input type="radio" name="client_type" id = "client2" value="External"  onclick="toggleExternal()"/> External
        </label>
        &nbsp 
        <input disabled type="text" id="extText" name="client_details2" value="rrrrrr"/> 
        <br><br>

        <div id="division">
        Division:
        <select name="client_details" id="client_details" onchange="clientDetails()">
            <option value="Choose"  />Choose Division...</option>
            <option value="Distribution"  />Distribution</option>
            <option value="Transmission"  />Transmission</option>
            <option value="Generation"  />Generation</option>
            <option value="Procument"  />Procument</option>
            <option value="Other"  />Others</option>
        </select>   
        <br><br>
        Others:<input type="text" name="client_details1" value="rrrrrr" id="client_details1" disabled/>
        <br>
        </div>
        <br>
        <input type="submit" name="submit" value="Submit"/>
    </form>     
</body>