admin管理员组文章数量:1417070
I have a form which posts data to a proccess.php file. In the form i am validating the input. There is a checkbox which asks if a tandem bicycle is used. I want it to display a text input field only if the box is ticked. here is my form code for that porion:
<tr>
<td>
<span style="font-weight:bold">Tandem:</span>
</td>
<td valign="middle">
<input type="checkbox" name="tandem">
<input style="visibility:hidden" type="text" name="partnerid" id="partnerid">
</td>
</tr>
Basically if checkbox "tandem" is checked, set text input field "partnerid" visibility:visible;
I have a form which posts data to a proccess.php file. In the form i am validating the input. There is a checkbox which asks if a tandem bicycle is used. I want it to display a text input field only if the box is ticked. here is my form code for that porion:
<tr>
<td>
<span style="font-weight:bold">Tandem:</span>
</td>
<td valign="middle">
<input type="checkbox" name="tandem">
<input style="visibility:hidden" type="text" name="partnerid" id="partnerid">
</td>
</tr>
Basically if checkbox "tandem" is checked, set text input field "partnerid" visibility:visible;
2 Answers
Reset to default 3I remend to use this code. When the disabled
property of an input element is true
, the element cannot be edited. The user usually has an indication that the input field should not be used.
<input type="checkbox" name="tandem" onchange="this.form.partnerid.disabled=!this.checked" />
Explanation of the code:
onchange=".."
is triggered when the value of the checkbox changes. This can either occur by clicking, or by using the keyboard. The click event is NOT sufficient for this purpose.this.form
from the context of an form element's event listener refers to the form
All named form elements can then be accessed throughthis.form.element_name
orthis.form["element_name"]
.- When the checkbox is checked (
checked==true
), the input should not be disabled (disabled=false
). This is done by using a negotiation:this.form.partnerid.disabled = !this.checked
.
Presumably the inputs are in a form, so you can put a click listener on the checkbox that makes the text input visible or not based on whether it's checked or not:
<input type="checkbox" name="tandem" onclick="
this.form.partnerid.style.visibility = this.checked? 'visible' : 'hidden';
">
It might seem logical to use the change event, but in IE that won't fire until the checkbox loses focus, which is a bit counter intuitive, so use click.
本文标签: javascriptDisplay a text input field only if another checkbox is checkedStack Overflow
版权声明:本文标题:javascript - Display a text input field only if another checkbox is checked - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745258045a2650219.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论