Javascript-onchange函数只在html select中工作一次(第一次更改后)
Javascript - onchange function only works once (after first change) with html select
我希望以下函数根据用户选择的客户类型显示不同的html输入。
以下代码只运行一次(当选择第一个选项时)
这是我的html:
<select name="customerType" id="customerType" onchange="return customerTypeFunction()">
<option value="">Customer Type?</option>
<option value="nonCorp">Customer</option>
<option value="corp">Corporate</option>
</select>
<div id="nonCorpCustDetails" class="custDetails">
Forename <input type="text" name="forename" id="forename"/>
Surname <input type="text" name="surname" id="surname"/>
</div>
<div id="corporateCustDetails" class="custDetails" style="visibility:hidden">
Company Name <input type="text" name="companyName" id="companyName"/>
</div>
这是我的javascript函数:
function customerTypeFunction() {
var customerTypeSelect = document.getElementById("customerType").value;
var corpCustomer = document.getElementById("corporateCustDetails");
var nonCorpCustomer = document.getElementById("nonCorpCustDetails");
if ( customerTypeSelect = 'nonCorp') {
corpCustomer.style.visibility = "hidden";
nonCorpCustomer.style.visibility = "visible";
}
if ( customerTypeSelect = 'corp' ) {
corpCustomer.style.visibility = "visible";
nonCorpCustomer.style.visibility = "hidden";
}
}
=
是赋值,==
/===
是比较。更改if语句以反映这一点。
if (customerTypeSelect === 'nonCorp') {
corpCustomer.style.visibility = "hidden";
nonCorpCustomer.style.visibility = "visible";
}
if (customerTypeSelect === 'corp') {
corpCustomer.style.visibility = "visible";
nonCorpCustomer.style.visibility = "hidden";
}
jsFiddle此处
相关文章:
- 使用每500ms运行一次的jquery函数是个好主意吗
- Javascript函数,需要每隔30秒递减一次
- 当元素可见时,jQuery滚动函数会触发一次
- 使函数只运行一次(Javascript)
- 如何在Javascript中设置函数在上一次结束时启动
- 运行“;非“;单击另一个函数中的函数仅一次
- 角函数每秒钟增加一次
- JavaScript只有在最后一次被调用时才运行函数
- 如何只使用一次window.onload函数
- 如何每 10 秒调用一次 JS 函数,然后以角度激活一个函数
- 节点 js 在启动时调用函数一次
- 有人可以帮助我调试带有addClass和removeClass函数的“每个会话一次”cookie吗?
- 如何在 window.setInterval 中每分钟运行一次 getJSON 函数
- 一次将多个对象传递给函数参数
- 就绪函数上的指令只能执行一次
- 一段时间后调用函数,但只调用一次
- 如何加载一个html页面并在其中只需单击一次即可运行函数
- Javascript只使用setInterval()调用一个子函数一次
- 元素在JavaScript函数中只更改过一次
- 函数只更改一次内容