如何在下拉列表中启用多个条件?
How to enable more than one condition in dropdown list?
我有一个下拉列表。当我select"Others"时,"Other Action textbox"会启用。
当我select"Classroom Training"或"On Job Training(OJT)"时,下拉"Proposed Training in Ilsas" 和文本框 "Proposed Training in Public" 将启用。
对于 "Others" 条件我已经做对了。
那么,如何在JavaScript中添加其他条件呢?这是因为我尝试将条件组合成JavaScript但是没有成功
<script language="javascript" type="text/javascript">
function otherAction(val) {
var otherAct = document.getElementById('otherAct');
if(val=='Others') {
otherAct.style.display = "block";
} else {
otherAct.style.display = "none";
}
}
</script>
<table>
<tr>
<td>Action: </td>
<td>
<select name="perAction" onchange="otherAction(this.value)">
<option value="0"></option>
<option value="Classroom Training">Classroom Training</option>
<option value="Coaches and Mentoring by IM">Coaches and Mentoring by IM</option>
<option value="On Job Training (OJT)">On Job Training (OJT)</option>
<option value="Others">Others</option>
</select>
</td>
<td>Other Action: </td>
<td><input name="otherAct" type="text" id="otherAct" /></td>
<td>Proposed Training in Ilsas: </td>
<td>
<select name = "perIlsas">
<option value="0"></option>
<option value="Project Management">Project Management</option>
<option value="Contract Management">Contract Management</option>
<option value="Analytical Skill">Analytical Skill</option>
</select>
</td>
<td>Proposed Training in Public: </td>
<td><input name="pubTraining" type="text" id="pubTraining" /></td>
</tr>
</table>
您只需使用多个 if-else
条件
<script language="javascript" type="text/javascript">
function otherAction(val) {
var otherAct = document.getElementById('otherAct');
if(val=='Others') {
otherAct.style.display = "block";
} else if(val == 'On Job Training (OJT)'){
otherAct.style.display = "none";
pubTraining.style.display = "block";
// set display here either `block` or `none` according to your need
}
else{
// set display here either block or none according to your need
}
}
</script>
编辑:
首先给 select 框一些 id
属性。然后,您可以编写另一个 else-if
条件检查,如下所示
<select id="perIlsas" name = "perIlsas"> //assigning id attribute
else if(val == 'Coaches and Mentoring by IM'){ //another else-if condition
$('#perIlsas').prop('disabled', 'disabled'); //this will disable entire selectbox
}
您可以在 jQuery Docs
中了解更多关于 prop
的信息
我有一个下拉列表。当我select"Others"时,"Other Action textbox"会启用。
当我select"Classroom Training"或"On Job Training(OJT)"时,下拉"Proposed Training in Ilsas" 和文本框 "Proposed Training in Public" 将启用。
对于 "Others" 条件我已经做对了。
那么,如何在JavaScript中添加其他条件呢?这是因为我尝试将条件组合成JavaScript但是没有成功
<script language="javascript" type="text/javascript">
function otherAction(val) {
var otherAct = document.getElementById('otherAct');
if(val=='Others') {
otherAct.style.display = "block";
} else {
otherAct.style.display = "none";
}
}
</script>
<table>
<tr>
<td>Action: </td>
<td>
<select name="perAction" onchange="otherAction(this.value)">
<option value="0"></option>
<option value="Classroom Training">Classroom Training</option>
<option value="Coaches and Mentoring by IM">Coaches and Mentoring by IM</option>
<option value="On Job Training (OJT)">On Job Training (OJT)</option>
<option value="Others">Others</option>
</select>
</td>
<td>Other Action: </td>
<td><input name="otherAct" type="text" id="otherAct" /></td>
<td>Proposed Training in Ilsas: </td>
<td>
<select name = "perIlsas">
<option value="0"></option>
<option value="Project Management">Project Management</option>
<option value="Contract Management">Contract Management</option>
<option value="Analytical Skill">Analytical Skill</option>
</select>
</td>
<td>Proposed Training in Public: </td>
<td><input name="pubTraining" type="text" id="pubTraining" /></td>
</tr>
</table>
您只需使用多个 if-else
条件
<script language="javascript" type="text/javascript">
function otherAction(val) {
var otherAct = document.getElementById('otherAct');
if(val=='Others') {
otherAct.style.display = "block";
} else if(val == 'On Job Training (OJT)'){
otherAct.style.display = "none";
pubTraining.style.display = "block";
// set display here either `block` or `none` according to your need
}
else{
// set display here either block or none according to your need
}
}
</script>
编辑:
首先给 select 框一些 id
属性。然后,您可以编写另一个 else-if
条件检查,如下所示
<select id="perIlsas" name = "perIlsas"> //assigning id attribute
else if(val == 'Coaches and Mentoring by IM'){ //another else-if condition
$('#perIlsas').prop('disabled', 'disabled'); //this will disable entire selectbox
}
您可以在 jQuery Docs
中了解更多关于prop
的信息