JavaScript - 简单查询 [英] JavaScript - Simple Query
问题描述
请参阅我已有的 JSFiddle 代码。我的问题是我需要使用无线电检查 AND 文本字段中的文本条目以启用其他无线电,复选框和文本字段。到目前为止,我只设法使用无线电选项1 OR 无线电选项2启用无线电,复选框和文本字段。
Please see the JSFiddle code I already have. My issue is that I require to use a radio check AND a text entry into a text field to enable other radios, checkboxes and text fields. So far I have only managed to enable radios, checkboxes and text fields using radio option 1 OR radio option 2.
此 JSFiddle 可以帮助你们,让你们更加了解我的意思。
This JSFiddle might help you guys out and give you a greater understanding of what I mean.
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 OR field2,具体取决于它们是否将radio-example_0或example_1分区我不是一个什么样的人如果他们选择是(example_0),然后在field1中输入交易名称,那么启用field3。
In this example the code currently enables and focuses on either field1 OR field2 depending on whether they section radio-example_0 or example_1. What I have not been able to figure out is if they select Yes (example_0) and then enter a trading name in field1 how then to enable field3.
希望这比我上一次尝试更清楚。谢谢你的帮助! (:
Hope this is clearer than my last attempt. Thanks for your help! (:
答案:请 Daniel V 。
$(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").blur(function(){
if($("#example_0").is(":checked")){
if($("#field1").val()!==""){
$("#field3").removeAttr("disabled");
}
}
});
推荐答案
是的,你可以制作一个看起来像的选择器对于选中的复选框和非空文本框:
Yes, you can make a selector that looks for the checked checkbox and a non-empty text box:
if ($('#example1:checked,#textfield1[value!=""]').length == 2) {
演示: http://jsfiddle.net/Guffa/rQKRH/
使用该HTML代码,您可以使用此代码确定是否应启用文本框:
With that HTML code you can use this to determine if the text box should be enabled:
var enable = $('#example_0').is(':checked') && $('#field1').val() != '';
演示: http://jsfiddle.net/Guffa/cEaeK/18/
这篇关于JavaScript - 简单查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!