[英]How to enable/disable drop down list box in HTML + Javascript/JQuery?
[英]Javascript - enable/disable elements and text box based on drop down not working
此處的代碼演示摘自該問題 。 但是,當從下拉列表中選擇值“其他”時,我想啟用文本框。
這是我從jsfiddle中編輯的代碼。
<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>
                                     
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" onclick="toggleExternal()"/> External
</label>
 
<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>
我添加了功能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>
                                     
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" onclick="toggleExternal()"/> External
</label>
 
<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>
您在此行中犯了一個錯誤。 在val == 4
您放置了“或”符號“ || ”,這在JS中是不允許的。
您的第二個錯誤是“ otherTxt ”字段已被禁用,您嘗試禁用它。 因此更改值“ disable = false ”
if (val == 0 || val == 1 ||val == 2 ||val == 3 ||val == 4)
{
document.getElementById("otherTxt").disabled = 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>
                                     
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" onclick="toggleExternal()"/> External
</label>
 
<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>
那對你有幫助。
<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>
                                     
<label for ="client2">
<input type="radio" name="client_type" id = "client2" value="External" onclick="toggleExternal()"/> External
</label>
 
<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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.