[英]checkbox to disable/enable input
我正在使用js來選中復選框的onclick,它啟用或禁用文本輸入。
它有效,直到我使用Google jquery api將其放入實時表單中為止。
很奇怪,但是..一定是某個地方的沖突。
表單元素是:(代碼未正確添加到此帖子中)
<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" /></div>
信用卡名稱(如果與上面不同)
js是:
function enable_text(status) { status=!status; document.form1.other_name.disabled = status; }
我在做什么錯,我使用了body onload處理程序。
<body onload=enable_text(false);>
JS FIDDLE: http : //www.jsfiddle.net/ozzy/H8VPY/4/
在這里,一個jQuery解決方案:
$("input:checkbox").click(function() {
$("input:text").attr("disabled", !this.checked);
});
只需將這些通用選擇器替換為您更具體的選擇器即可。
除此代碼外,您可能還希望使文本框(最初)處於禁用狀態。
<input type="text" disabled="disabled" />
工作演示: http : //www.jsfiddle.net/H8VPY/11/
您的jsfiddle演示中有幾個問題。
<script>
及其周圍的注釋應被刪除。 <form name="form1">
,這導致document.form1
不返回任何內容。 更新的演示: http : //www.jsfiddle.net/PPZYm/
function enable_text(status) {
status = (status) ? false : true; //convert status boolean to text 'disabled'
document.form1.other_name.disabled = status;
}
注意
您還需要使用名稱為form1
的<form>
標記將div
包裝在jsfiddle示例中,以使其正常工作
<div class="controlset-pad">
<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" />
</div>
<form name="form1">
<div class="field4">
<label>Name on credit card if different from above</label><input type="text" name="other_name" class="medium" disabled="disabled" />
</div>
</form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.