如何基于单选按钮切换启用/禁用输入字段
How to toggle enable/disable input field based on radio button
我有一组单选按钮,带有lase选项"其他"和一个输入field type text
。默认情况下,输入字段被禁用,如果您单击"其他"单选按钮,则该字段将被启用。如果单选按钮上的选择发生更改,如何禁用输入字段?
这是我的代码:
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});
如果您更改收音机选择,此代码不会禁用输入字段。
更新1我正在尝试这个:
$(document).ready(function(){
$("#text-inputt").prop('disabled', true);
});
$("#lastt").click(function(){
if($(this).is(':checked')) {
$("#text-inputt").prop("disabled", false);
} else {
$("#text-inputt").prop("disabled", true);
}
});
但如果您更改单选按钮,则不会将输入设置为禁用。
你可以这样做
$(document).ready(function(){
$("#text-inputt").prop('disabled', true);
$('input[type=radio]').click(function(){
if($(this).prop('id') == "lastt"){
$("#text-inputt").prop("disabled", false);
}else{
$("#text-inputt").prop("disabled", true);
}
});
});
更改:
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){ $("#text-inputt").prop('disabled', false/true)});
至
$(document).ready(function(){
$("#text-inputt").prop('disabled', true)
});
$("#lastt").click(function(){
if($(this).val=='Other')
{
$("#text-inputt").prop('disabled', false)
}
else
{
$("#text-inputt").prop('disabled', true)
}
});
试试这个,这里的输入框将切换,即启用/禁用
$("#radioBtn").change(function(){
$("#inputBox").attr("disabled", ! $(this).is(':checked'));
});
解决方案可以是:
- 将
change
侦听器添加到单选按钮组 - 当收音机发生变化时,检查其值
- 如果值为"other",请启用输入字段
工作示例:https://jsfiddle.net/3afjqe7j/1/
var $input = $('#text-inputt');
$("input[name=my-radios]").on('change', function() {
var disabled = $(this).val() !== 'other';
$input.prop('disabled', disabled);
});
相关文章:
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在HTML输入字段中添加不可删除的后缀
- 互斥单选按钮和相应的输入字段
- 在IE9中的输入字段中输入焦点最近按钮
- 选中单选框时将属性添加到输入字段
- 当设置addFromAutocompleteOnly时,剩余文本将保留在输入字段中
- 我如何在数字插入中使用逗号,这样它就不会'不要破坏我的输入字段
- 在输入字段上有两个函数调用,一个在Blur上,一个不在Angular中
- 输入字段将't获取更新的值
- 如何选择多个输入字段并删除所需的属性
- 输入字段,只接受0到12之间的数字
- 单击鼠标,用MySQL数据填充html表单输入字段
- 使用jquery将输入字段转换为文本
- Model中的Typeahead返回空值以形成输入字段
- 如何从查询字符串中的输入字段发回文本
- 如何验证日期、月份和日期的3个独立输入字段;年使用jquery或javascript
- 将值传递给jquery创建的输入字段