JavaScript查询-使用AND &OR使用无线电、复选框和文本字段来启用表单元素
JavaScript Query - Using AND & OR with Radios, Checkboxes and Text Fields to enable form elements
请参阅我已经拥有的JSFiddle代码。我的问题是,我需要使用单选检查和文本输入到文本字段中,以启用其他单选、复选框和文本字段。到目前为止,我只使用单选选项1 或单选选项2来启用无线电,复选框和文本字段。
这个JSFiddle可能会帮助你们,让你们更好地理解我的意思。
HTML: <div class='conlabel'>Have you started trading yet?</div>
<table width="100">
<tr>
<td><label>
<input type="radio" name="example" value="Yes" id="example_0" required />
Yes</label></td>
</tr>
<tr>
<td><label>
<input type="radio" name="example" value="No" id="example_1" required />
No</label></td>
</tr>
</table><br>
<li>
<div class='conlabel'>If Yes, enter trading name:</div>
<input type="text" id="field1" name="field1" placeholder="" disabled />
</li><br>
<li>
<div class='conlabel'>If No, then:</div>
<input type="text" id="field2" name="field2" placeholder="" disabled />
</li><br>
<li>
<div class='conlabel'>Enter trading start date (enabled when started trading yet = yes + trading name = notnull:</div>
<input type="text" id="field3" name="field3" placeholder="" disabled />
</li><br>
JS:
$(function(){
$("#example_0, #example_1").change(function(){
$("#field1, #field2").val("").attr("disabled",true);
if($("#example_0").is(":checked")){
$("#field1").removeAttr("disabled");
$("#field1").focus();
}
else if($("#example_1").is(":checked")){
$("#field2").removeAttr("disabled");
$("#field2").focus();
}
});
});
在本例中,代码当前启用并关注field1 或 field2,这取决于它们是划分radio-example_0还是example_1。我无法弄清楚的是,如果他们选择Yes (example_0),然后在field1中输入一个交易名称,那么如何启用field3。
希望这比我上次的尝试更清楚。谢谢你的帮助!(:
您需要在光标离开时检查field1的值,试试这个:
$("#field1").blur(function(){
if($("#example_0").is(":checked")){
if($("#field1").val()!==""){
$("#field3").removeAttr("disabled");
}
}
});
http://jsfiddle.net/cEaeK/13/$(function(){
$("#example_0, #example_1").change(function(){
$("#field1, #field2").val("").attr("disabled",true);
if($("#example_0").is(":checked")){
$("#field1").removeAttr("disabled");
$("#field1").focus();
}
else if($("#example_1").is(":checked")){
$("#field2").removeAttr("disabled");
$("#field2").focus();
$("#field3").val('');
$("#field3").attr("disabled",true);
}
});
$('#field1').focusout(function(){
if($('#field1').val().length!=0)
$("#field3").removeAttr("disabled");
else
{ $("#field3").val('');
$("#field3").attr("disabled",true);
}
});
});
if(($("#example0").is(":checked") && $("#field0").val().length != 0) || ($("#example1").is(":checked") && $("#field1").val().length != 0)) {
PASS!
}
相关文章:
- 复选框与文本内联-JS问题
- 根据文本值禁用复选框
- 使用javascript提交表单并从字段/单选框/复选框/文本区域获取数据
- Javascript选中/取消选中所有复选框并将值写入文本区域
- 获取文本框中所有选定复选框的值
- 动态选中文本区域中现有项目的复选框组
- 选中复选框时更改文本框值
- 单击每个表格行中的复选框时启用文本框
- jQuery表 - 如何根据选中的复选框在单元格中写入文本
- 在 AngularJs 中触发文本框的复选框
- 基于复选框 Jquery 更新文本框
- 根据复选框选择 jQuery 更新文本框
- 我无法禁用带有复选框的文本框
- 从选中的复选框中获取所有值,并将其放入文本区域
- 使用复选框列表中的jquery附加文本
- 在其他文本输入字段中输入文本时启用复选框
- 选中复选框时清除填充有datatime选取器数据的文本框
- 将文本值与复选框值之和相乘,并将其显示在段落中
- 使用复选框和js输入文本
- j查询选中了同级文本复选框