Dependent checkbox in array input field

I have two flags for the array, namely chk1 and chk2, this flag data is generated dynamically. When I check chk1 UK , I need to check chk2 90 . if US means 60 , then check and so on. Any suggestion for this task?

<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN

<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
+4
source share
4 answers

while you marked jquery you can use .eq()and.index()

$('input[name=chk1]').on('click' , function(){
    if($(this).is(":checked")){
     $('input[name=chk2]').eq($(this).index()).prop('checked' , true);
  }else{
    $('input[name=chk2]').eq($(this).index()).prop('checked' , false);
  }
});

Working demo

Note. be sure to enable jquery

Another way: selector :eq()with.index()

 $('input[name=chk2]:eq('+$(this).index()+')')
+2
source

. , .

$("input[type=checkbox][name=chk1]").change(function () {
    var status = this.checked;
    $("input[type=checkbox][name=chk2]").eq($(this).index()).prop('checked', status);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN

<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
Hide result
0

JavaScript

<input onclick='handleClick(this);' type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input onclick='handleClick(this);' type="checkbox" name="chk2" id="chk2" value="US"> US
<input onclick='handleClick(this);' type="checkbox" name="chk3" id="chk3" value="IN"> IN

<input type="checkbox" name="chk4" id="chk4" value="90"> 90
<input type="checkbox" name="chk5" id="chk5" value="60"> 60
<input type="checkbox" name="chk6" id="chk6" value="10"> 10
<script>
  function handleClick(cb) {

    switch (cb.name) {
      case "chk1":
        document.getElementById("chk4").checked = cb.checked;
        break;
      case "chk2":
        document.getElementById("chk5").checked = cb.checked;
        break;
      case "chk3":
        document.getElementById("chk6").checked = cb.checked;
        break;
    }
  }

</script>
0

$(document).on('click' ,'input[name=chk1]', function(){
	$("input[name=chk1]").each(function(i) {
		if($(this).is(":checked"))
		  $('input[name=chk2]:eq('+i+')').prop('checked' , true);
		else
		  $('input[name=chk2]:eq('+i+')').prop('checked' , false);
	});
    
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<input type="checkbox" name="chk1" id="chk1" value="UK"> UK
<input type="checkbox" name="chk1" id="chk1" value="US"> US
<input type="checkbox" name="chk1" id="chk1" value="IN"> IN
<br>
<input type="checkbox" name="chk2" id="chk2" value="90"> 90
<input type="checkbox" name="chk2" id="chk2" value="60"> 60
<input type="checkbox" name="chk2" id="chk2" value="10"> 10
Hide result
0

Source: https://habr.com/ru/post/1621946/


All Articles