[英]Hide/Unhide a table row with text input depending on the selected dropdown option using Javascript
Please help. 请帮忙。 If I select Business/Corporate from the dropdown, the table row is visible (which is correct), but when I reselect Residential/Consumer, the row does not hide. 如果从下拉列表中选择“企业/公司”,则表行可见(正确),但是当我重新选择“住宅/消费者”时,该行不会隐藏。 All I want is to be able to show the row if Business/Corporate is selected (so that one can enter the business name from the input text field) and hide when not selected. 我所希望的是,如果选择了“企业/公司”,则能够显示该行(以便可以从输入文本字段中输入企业名称),并在未选择时隐藏。 Thanks. 谢谢。 :) :)
My JS: 我的JS:
<script language="JavaScript">
function toggle(target)
{
if (target!="") {
obj=document.getElementById(target);
obj.style.display=( (obj.style.display=='none') ? '' : 'none');
}
}
</script>
My select option: 我的选择选项:
<select name="xaccttype" OnChange="toggle(this.value)">
<option value="Residential/Consumer">Residential/Consumer
<option value="Business/Corporate">Business/Corporate
</select>
My table row: 我的表格行:
<tr id="Business/Corporate" style="display:none"><td width="200" align="right"><b>Business Name:</b></td><td width="650"><input type="text" size="35" name="xbusinessname"></td></tr>
Give some different id
to the tr
. 给tr
赋予一些不同的id
。 Then do the following in the script. 然后在脚本中执行以下操作。 Make sure you do not use /
for id
names. 确保您不对id
名称使用/
。
Since, you do not have other row with the id
as Residential_Consumer
, it would not be feasible to check the target dynamically in the script. 由于您没有其他id
为Residential_Consumer
,因此无法在脚本中动态检查目标。
function toggle(target) { console.log("inside"); console.log(target); if (target!="") { obj=document.getElementById("row"); console.log(obj); if(target === "Business_Corporate") obj.style.display= 'block'; else obj.style.display= 'none'; } }
<select name="xaccttype" OnChange="toggle(this.value)"> <option value="Residential_Consumer">Residential/Consumer</option> <option value="Business_Corporate">Business/Corporate</option> </select> <table> <tr id="row" style="display:none"> <td width="200" align="right"><b>Business Name:</b></td> <td width="650"><input type="text" size="35" name="xbusinessname"></td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.