3

请参阅我已经拥有的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();   
    }
});
});

在此示例中,代码当前启用并关注 field1field2,具体取决于它们是 radio-example_0 还是 example_1。我无法弄清楚的是,如果他们选择是(example_0),然后在字段 1 中输入交易名称,那么如何启用字段 3。

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

4

3 回答 3

2

您需要在光标离开时检查 field1 的值,试试这个:

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

http://jsfiddle.net/cEaeK/13/

于 2013-03-21T10:55:11.553 回答
2
$(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);
            }

    });

});
于 2013-03-21T10:37:00.810 回答
1
if(($("#example0").is(":checked") && $("#field0").val().length != 0) || ($("#example1").is(":checked") && $("#field1").val().length != 0)) {
    PASS!
}
于 2013-03-21T09:59:23.617 回答