选中复选框时启用文本字段
enable the text field when check box is checked
$("#owncar").click(function() {
if ($(this).is(':checked')) {
$("#carnumber").attr('disabled', false);
} else {
$("#carnumber").val('');
$("#carnumber").attr('disabled', true);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<tr>
<td>Have own car :</td>
<td>
<input type="checkbox" name="owncar" id="owncar" value="Y" />
</td>
</tr>
<tr>
<td>Car Number :</td>
<td>
<input type="text" name="carnumber" id="carnumber" maxlength="10" disabled/>
</td>
</tr>
当用户从拥有自己的汽车中勾选复选框时,启用车号文本字段以允许用户填写车号。
上面的代码对我来说没问题。单击复选框时,还有其他方法可以启用文本字段吗?
使用 prop()
设置复选框的checked
状态。若要设置文本框的值,请使用带回调的val()
。
// Use change event on the checkbox
$("#owncar").change(function() {
// Cache checked status
var checked = this.checked;
// If checkbox is checked, enable the textfield
// else disable
$("#carnumber").prop('disabled', !checked).val(function(i, oldVal) {
// If the checkbox is not checked, then empty the value
return checked ? oldVal : '';
});
});
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 使用jquery将输入字段转换为文本
- 如何从查询字符串中的输入字段发回文本
- 表单输入字段随着溢出的文本而增长
- 使用单个文本框向多个字段添加严格搜索
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- 使用jQuery的输入字段文本换行
- 向使用jQuery加载DOM后添加的字段添加不受限制的文本输入DatePickers
- 当用户单击按钮(在光标位置)时,在输入字段中添加一个文本字符串
- 如何根据文本长度立即显示和隐藏字段?-JQuery
- 如何使文本、数字和日期html输入字段以一致的方式支持Ctrl+Z(撤消)
- 模拟文本输入字段上的退格键
- 输入字段中的可持续文本
- 未获取文本输入字段的值
- 没有文本安全性的密码字段
- 将禁用的输出字段更改为带前缀的文本
- 为什么 react 将输入[类型=“文本”] 字段设为只读,除非我提供 onChange 回调
- 使用SAPUI5中的“文本”字段显示正确的“日期”
- Kendo,如何将MVVM文本:字段绑定到远程DataSource