[英]how to solve on/off switch javascript
我的on / off開關有問題。我有一個代碼無法正常工作,因為當我以表格形式編寫check時它可以工作,但是我想拖拉心情,on and off,當不寫檢查時它的形式不起作用,當我在兩者中都簽入時,它不起作用,當我在其中之一中簽入時,它起作用但不正確。:(
<script>
function DisEn(X){
document.A.T1.disabled=X;
document.A.T2.disabled=X;
}
</script>
</head>
<form name="A" method="get" action="switch.php">
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" value="off" class="onoffswitch-checkbox"
id="myonoffswitch" onClick="DisEn(false)" checked>
<input type="checkbox" name="onoffswitch" value="on" class="onoffswitch-checkbox"
id="myonoffswitch1" onClick="DisEn(false)" >
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<input type="text" name="T1" value="A" disabled>
<input type="text" name="T2" value="B" disabled>
<input type="submit" name="T4" value="Submit" >
</form>
使用onchange = DisEn(this)
例如以下示例:
<script>
function DisEn(element){
if (element.checked === true) {
document.A.T1.disabled=true;
document.A.T2.disabled=true;
element.value="off";
} else {
document.A.T1.disabled=false;
document.A.T2.disabled=false;
element.value="on";
}
}
</script>
<form name="A" method="get" action="switch.php">
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" value="off"
class="onoffswitchcheckbox" id="myonoffswitch"
onchange="DisEn(this)" checked = "checked" />
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<input type="text" name="T1" value="A" disabled = "disabled" />
<input type="text" name="T2" value="B" disabled = "disabled" />
<input type="submit" name="T4" value="Submit" />
</form>
您可以在此處查看演示 。 如果出現問題或忘記做某事,請通知我,然后更新演示。
使用此功能,您只需在復選框上使用onchange = DisEn(this)
以為每個復選框進行設置。
HTML:
<form name="A" method="get" action="switch.php">
<div class="onoffswitch">
<input type="checkbox" name="onoffswitch" value="off"class="onoffswitchcheckbox"
id="myonoffswitch" onClick="DisEn()" checked>
<label class="onoffswitch-label" for="myonoffswitch">
<div class="onoffswitch-inner"></div>
<div class="onoffswitch-switch"></div>
</label>
</div>
<input type="text" name="T1" value="A" disabled>
<input type="text" name="T2" value="B" disabled>
<input type="submit" name="T4" value="Submit" >
</form>
Javascript:
function DisEn(){
if(document.A.onoffswitch.checked == false)
{
document.A.T1.disabled=false;
document.A.T2.disabled=false;
}
else
{
document.A.T1.disabled=true;
document.A.T2.disabled=true;
}
}
這是jsfiddle >> http://jsfiddle.net/YgWku/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.