Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have this code in my view:
C#
@foreach (WebStill.Rola r in Model.Role)
{
     @Html.CheckBoxFor(p => r.Selektovana, 
       new { @RolaID =r.RolaID,@class="cbRadnikRole",data_id=r.RolaID }); @r.Naziv
}


Which outputs this html:
HTML
<input class="cbRadnikRole" data-id="1" data-val="true" data-val-required="The Selektovana field is required." id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />Administrator                    <br />
<input class="cbRadnikRole" data-id="2" id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />Serviser                    <br />
<input class="cbRadnikRole" data-id="3" id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />Supervizor                    <br />
<input class="cbRadnikRole" data-id="4" id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />Vozač                    <br />
<input class="cbRadnikRole" data-id="5" id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />CHIEF supervizor                    <br />
<input class="cbRadnikRole" data-id="6" id="r_Selektovana" name="r.Selektovana" type="checkbox" value="true" /><input name="r.Selektovana" type="hidden" value="false" />Operater                    <br />


and this jQuery:
JavaScript
$(document).ready(function () {
        $(".cbRadnikRole").change(function () {
            if ($(".cbRadnikRole:checked").data('id') == 2) {
                $('#ulOrgJedinice').show();
            }
            else {
                $('#ulOrgJedinice').hide();
                $('#ulOrgJedinice').find("input").attr('checked', false);
            }
        });

        $(".cbRadnikRole").change();
    });


This works in Opera and Firefox,but IE and Chrome are not reading data-id correctly.
For example when i click on check box with data-id 1,i get 2,or sometimes 3 or other id.
I have similar code in other view,but i use this insted of cbRadnikRole:checked,which i cant use here because i need to trigger it when page loads.


Any suggestions ?
Posted

Put down this 2 code above your title tag in head section.
HTML
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

because due to compatibility issues it might not work so above code can help to solve that layer of issue.
 
Share this answer
 
v2
@Akinmade Bond
I tried that,and every other combination i could think of but the problem is in the selector
JavaScript
$(".cbRadnikRole:checked")

It gets it right only the first time,and after that is like its late or something.I fixed it,but it has some code duplication.I manually hide what i want outside of the event handler.

JavaScript
 $(document).ready(function () {

if ($(".cbRadnikRole:checked").data('id') == 2) {
   $('#ulOrgJedinice').show();
}
else {
   $('#ulOrgJedinice').hide();
   $('#ulOrgJedinice').find("input").attr('checked', false);
}

$(".cbRadnikRole").change(function () {
if ($(this).data('id') == 2) {
   $('#ulOrgJedinice').show();
}
else {
   $('#ulOrgJedinice').hide();
   $('#ulOrgJedinice').find("input").attr('checked', false);
}
});


When i use 'this' it works as it should.
 
Share this answer
 
Comments
Akinmade Bond 5-Oct-13 14:13pm    
See my edited solution.
EDIT:

JavaScript
$(document).ready(function () {
    $(".cbRadnikRole").each(function (index) {
        $(this).on('change', function(val){
            $(this).prop( "checked", function( i, val ) {
                if ( val ) {
                    var id = $(this).attr('data-id');					
                    
                    if ( id == 2 ) {
                        checker(true);
                    }
                } else {
                    checker(false);
                }
            });			
        });
    });
		
    function checker (val) {
        if (val) {
            $('#ulOrgJedinice').show();
        } else {
            $('#ulOrgJedinice').find("input").attr('checked', false);
            $('#ulOrgJedinice').hide();
        }
    }
});
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900