JavaScript - 简单查询

时间:2013-03-21 08:43:00

标签: javascript jquery

请参阅我已有的JSFiddle代码。我的问题是我需要使用无线电检查 AND 文本字段中的文本条目以启用其他无线电,复选框和文本字段。到目前为止,我只使用无线电选项1 OR 无线电选项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 OR field2,具体取决于它们是否包含radio-example_0或example_1。我无法弄清楚的是,如果他们选择是(example_0)然后在field1中输入交易名称,那么如何启用field3。

希望这比我上一次尝试更清楚。谢谢你的帮助! (:

答案: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");
            }
        }
    });

3 个答案:

答案 0 :(得分:1)

是的,你可以创建一个查找复选框和非空文本框的选择器:

if ($('#example1:checked,#textfield1[value!=""]').length == 2) {

演示:http://jsfiddle.net/Guffa/rQKRH/

编辑:

使用该HTML代码,您可以使用它来确定是否应启用文本框:

var enable = $('#example_0').is(':checked') && $('#field1').val() != '';

演示:http://jsfiddle.net/Guffa/cEaeK/18/

答案 1 :(得分:0)

我不确定我是否理解你,但你的意思是这样吗?

 if(($("#example1").is(":checked"))&&!$('#input').val()){//do}

演示:http://jsfiddle.net/WULPL/1/

答案 2 :(得分:0)

 $("#field1").blur(function(){
   if($("#example_0").is(":checked")){
            if($("#field1").val()!==""){
                  $("#field3").removeAttr("disabled");
            }
        }
    });