2017-06-29 5 views
-1

少なくとも1つのチェックボックスをチェックするためにjQueryの検証コードを実装したいと思います。チェックボックスがオンになったら、一致する入力テキストボックスに値を入力する必要があります。ここでブートストラップjQuery html5チェックボックスとテキストボックスの複合チェックコード

<form id="demo" class="m-t" role="form" method="post" action="register.php"> 
<label class="checkbox-inline"> 
<input type="checkbox" id="is_a1" name="is_a1" value="1">a1</label> 

<label class="checkbox-inline"> 
<input type="checkbox" id="is_a2" name="is_a2" value="1">a2</label> 

<label class="checkbox-inline"> 
<input type="checkbox" id="is_a3" name="is_a3" value="1">a3</label> 
<input type="text" name="a1_input" id="a1_input" value=""> 
<input type="text" name="a2_input" id="a2_input" value=""> 
<input type="text" name="a3_input" id="a3_input" value=""> 
<button type="submit" class="btn btn-default block full-width m-b">submit</button> 

</form> 
<script> 
$(document).ready(function() { 
    $("#demo").submit(function(event) { 
     if ($("#a1").is(':checked')){ 
      if ($("#a1_input).val().length > 0) { 
       return true; 
      } 
      else { 
      alert("a1_input is empty"); 
      return false; 
      } 
     } 
     if ($("#a2").is(':checked')){ 
     if ($("#a2_input).val().length > 0) { 
       return true; 
      } 
      else { 
      alert("a2_input is empty"); 
      return false; 
      } 
     } 
     if ($("#a3").is(':checked')){ 
      if ($("#a3_input).val().length > 0) { 
       return true; 
      } 
      else { 
      alert("a3_input is empty"); 
      return false; 
      } 
     } 
     alert("please check at least one check box"); 

    }); 

    }); 
</script> 
+0

を使用すると、複雑な検証コードによってどのような意味ですか? –

答えて

0

ちょうどチェックボックスを反復処理し、checkedプロパティを確認しますCK8 @

$('#demo .btn').on('click', function() { 
    var checked = []; 
    $('#demo input[type="checkbox"]').each(function (idx, obj) { 
     if ($(this).prop("checked") === true) { 
      var parts = $(this).attr('name').split("_"); 
      checked.push(parts[1]); 
     } 
    }); 
    if (checked.length > 0) { 
     $.each(checked, function (idx, val) { 
      if ($('#' + val + '_input').val() === "") { 
       alert(val + ' checked but no input'); 
      } 
     }); 
    } else { 
     alert('nothing checked'); 
    } 
}); 

https://jsfiddle.net/mkkmvrj3/2/

+0

チェックボックスのコードはありがたくです。一致する入力テキストボックスをチェックし、それが空でないことを確認する方法はありますか? – CK8

+0

@ CK8コードが更新されました – vaso123

0

は時間のために、私はis_a1からa1aタグIDを変更して、あなたのソリューションです。ワーキングデモ:https://jsfiddle.net/mgfzhbt4/1/

$('input[type="text"]').prop('disabled',true); 
$('input[type="checkbox"]').change(function(){ 
    if($(this).is(':checked')) 
    { 
    var id = $(this).attr('id'); 
    alert($(this).attr('id')); 
    $('#'+id+'_input').prop('disabled',false); 
    }else 
    { 
    var id = $(this).attr('id'); 
    $('#'+id+'_input').html(''); 
    $('#'+id+'_input').prop('disabled',true); 
    } 
}) 

$('#submitBtn').click(function(e){ 
    if(!$('textbox').is(':disabled')) 
    { 
    if($(this).val().length == 0) 
    { 
     alert('Field empty'); 
    e.preventDefault(); 
    } 

    } 
}) 
+0

私はa1をチェックし、最初のテキストボックスに何かを入力して[送信]ボタンをクリックしても何も送信されませんでした。 – CK8

+0

それはあなたの質問ではなかった、私は答えた – Rahul

+0

そしてあなたはサーバー側に正しいコードを書いています – Rahul

関連する問題