JavaScript查询 – 使用AND和OR与无线电,复选框和文本字段来启用表单元素
请参阅我已有的JSFiddle代码。 我的问题是我需要在文本字段中使用无线电检查和文本输入来启用其他无线电,复选框和文本字段。 到目前为止,我只使用无线电选项1 或无线电选项2设法启用无线电,复选框和文本字段。
这个JSFiddle可以帮助你们,让你们更加了解我的意思。
HTML:
Have you started trading yet?
If Yes, enter trading name:
If No, then:
Enter trading start date (enabled when started trading yet = yes + trading name = notnull:
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 OR field2,具体取决于它们是否将radio-example_0或example_1分区。 我无法弄清楚的是,如果他们选择是(example_0)然后在field1中输入交易名称,那么如何启用field3。
希望这比我上一次尝试更清楚。 谢谢你的帮助! (:
你需要在光标离开时检查field1的值,试试这个:
$("#field1").blur(function(){ if($("#example_0").is(":checked")){ if($("#field1").val()!==""){ $("#field3").removeAttr("disabled"); } } });
$(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! }