[英]checkbox to disable/enable input
I am using js to , onclick of a checkbox, it enables or disables text input. 我正在使用js来选中复选框的onclick,它启用或禁用文本输入。
It works, until I put it into a live form with google jquery api. 它有效,直到我使用Google jquery api将其放入实时表单中为止。
Weird but.. must be a conflict somewhere. 很奇怪,但是..一定是某个地方的冲突。
The form element is: ( code isnt adding to this post properly ) 表单元素是:(代码未正确添加到此帖子中)
<input type="checkbox" name="others" onclick="enable_text(this.checked)" class="medium" /></div>
Name on credit card if different from above 信用卡名称(如果与上面不同)
The js is: js是:
function enable_text(status) { status=!status; document.form1.other_name.disabled = status; }
What am I doing wrong, I have used body onload handler. 我在做什么错,我使用了body onload处理程序。
<body onload=enable_text(false);>
JS FIDDLE : http://www.jsfiddle.net/ozzy/H8VPY/4/ JS FIDDLE: http : //www.jsfiddle.net/ozzy/H8VPY/4/
Here, a jQuery solution: 在这里,一个jQuery解决方案:
$("input:checkbox").click(function() {
$("input:text").attr("disabled", !this.checked);
});
Just replace these generic selectors with your more specific ones. 只需将这些通用选择器替换为您更具体的选择器即可。
In addition to this code, you will probably also want to make the text-box disabled (initially). 除此代码外,您可能还希望使文本框(最初)处于禁用状态。
<input type="text" disabled="disabled" />
Working demo: http://www.jsfiddle.net/H8VPY/11/ 工作演示: http : //www.jsfiddle.net/H8VPY/11/
There are several problems in your jsfiddle demo. 您的jsfiddle演示中有几个问题。
<script>
and the comment there around should be removed. <script>
及其周围的注释应被删除。 <form name="form1">
is missing which caused document.form1
to return nothing. <form name="form1">
,这导致document.form1
不返回任何内容。 Updated demo: http://www.jsfiddle.net/PPZYm/ 更新的演示: 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;
}
Note 注意
You also need to wrap your div
in the jsfiddle example with a <form>
tag with the name form1
for it to properly work 您还需要使用名称为
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.