Как получить доступ к одному div внутри другого div - PullRequest
0 голосов
/ 14 марта 2020

У меня есть два div, когда я нажимаю на первый флажок div, я хочу использовать значение второго div.

var NewData = '<div class="divClassOne"> &nbsp; 
<input type="checkbox" class="chkremCFAllLevel1" value="HelloOne" name="chkOne" 
id="check1" data-markerAllLevel1="12"></div>';
$("#sectionlistNew").append(NewData);
var NewDataTwo = '<div class="divClassTwo"> &nbsp; 
<input type="checkbox" class="chkremCFAllLevel2" value="HelloTwo" name="chkTwo" 
id="check2" data-markerAllLevel2="12"></div>';
$("#sectionlistNewTwo").append(NewDataTwo);
$("#sectionlistNew").on('click', '.chkremCFAllLevel1', function () {
            marker_refAllLevel1 = $(this).attr('data-markerAllLevel1');
            console.log(marker_refAllLevel1);
});

Когда я нажимаю, я хочу сравнить data-markerAllLevel1 и data-markerAllLevel2 значение.

Решение

$("#sectionlistNew").on('click', '.chkremCFAllLevel1', function () {
            checkedvalueAllCheckBoxLevel1 = [];
            marker_refAllLevel1 = $(this).attr('data-markerAllLevel1');
            console.log(marker_refAllLevel1);

            $("input[name=chkRolesALLLevel2]").each(function () {
                if ($(this).attr("data-markerCheckBoxAllLevel2") == marker_refAllLevel1) {
                    console.log($(this).attr("data-markerCheckBoxAllLevel2"));
                    $('input[name="chkRolesALLLevel2"][data-markerCheckBoxAllLevel2="' + marker_refAllLevel1 + '"]').attr('disabled', false);
                }
            });
        });

Ответы [ 2 ]

1 голос
/ 15 марта 2020

Код:

var NewData = '<div class="divClassOne"> &nbsp;<input type="checkbox" class="chkremCFAllLevel1" value="HelloOne" name="chkOne" id="check1" data-markerAllLevel1="12"></div>';
$("#sectionlistNew").append(NewData);
var NewDataTwo = '<div class="divClassTwo"> &nbsp;<input type="checkbox" class="chkremCFAllLevel2" value="HelloTwo" name="chkTwo" id="check2" data-markerAllLevel2="12"></div>';
$("#sectionlistNewTwo").append(NewDataTwo);
$("#sectionlistNew").on('click', '.chkremCFAllLevel1', function() {
  marker_refAllLevel1 = $(this).attr('data-markerAllLevel1');
  marker_refAllLevel2 = $('#check2').attr('data-markerAllLevel2');;
  if (marker_refAllLevel1 == marker_refAllLevel1) {
    console.log('Its both are same' + marker_refAllLevel1 + '=' + marker_refAllLevel2);
  } else {
    console.log('Its both are not same');
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="sectionlistNew"></div>
<div id="sectionlistNewTwo"></div>
1 голос
/ 14 марта 2020

Вы должны объяснить больше, что вы пытаетесь сделать.
HTML:

<input type="checkbox" class="chkremCFAllLevel1" value="HelloOne" name="chkOne" id="check1" data-markeralllevel1="12">
<input type="checkbox" class="chkremCFAllLevel2" value="HelloTwo" name="chkTwo" id="check2" data-markeralllevel2="12">

Jquery:

$(document).ready(function() {
    $("input[data-markerAllLevel1]").change(function () {
        let markerAllLevel1 = $(this).data('markeralllevel1')
        let markerAllLevel2 = $('[data-markeralllevel2]').data('markeralllevel2')

        console.log(`${markerAllLevel1} == ${markerAllLevel2}`)

        if(markerAllLevel1 == markerAllLevel2){ /* compare here whatever you need (not sure what you mean) */
            if($(this).is(':checked')){
                $('input[data-markeralllevel2]').prop('checked', true);
            } else {
                $('input[data-markeralllevel2]').prop('checked', false);
            }
        }

        /* if you need to set value to another one, use this */
        $('input[data-markeralllevel2]').val($(this).val())


        console.log($('input[data-markeralllevel2]').val())
    });

    $("input[data-markeralllevel2]").change(function () {
        let markerAllLevel1 = $('input[data-markeralllevel1]').data('markeralllevel1')
        let markerAllLevel2 = $(this).data('markeralllevel2')

        console.log(`${markerAllLevel1} == ${markerAllLevel2}`)

        if(markerAllLevel1 == markerAllLevel2){ /* compare here whatever you need (not sure what you mean) */
            if($(this).is(':checked')){
                $('input[data-markeralllevel1]').prop('checked', true);
            } else {
                $('input[data-markeralllevel1]').prop('checked', false);
            }
        }

        /* if you need to set value to another one, use this */
        $('input[data-markeralllevel1]').val($(this).val())

        console.log($('input[data-markeralllevel1]').val())
    });
});
...