Sometimes we need to work with CheckBox and we have faced many problem to manage functionality of CheckBox.
Now I Will give you an example of CheckBox functionality. I think it will be helpfull.
----------UI-------------------
Create an Input Item in html :
<div>
<ul>
<li>
<label for="IsActive" class="lbl widthSize20_per">Is Active:</label>
<input type="checkbox" id="chkIsActive" name="IsActive" />
</li>
</ul>
</div>
----------Javascript-------------------
How to get value from CheckBox:
var IsActive = $("#chkIsActive").is(":checked") == true ? 1 : 0;
----Fill Checkbox-------------
var data = GetDataFromServer();
if (data.IsActive == 1) {
$("#chkIsActive").prop('checked', 'checked');
} else {
$("#chkIsActive").removeProp('checked', 'checked');
}
--------is checked event------------------
CheckBox click event:
$('#chkIsActive').live('click', function (e) {
var $cb = $(this);
if ($cb.is(":checked")) {
//do something
} else {
//do something
}
});
Clear CheckBox:
---------------------------------------------------------
$("#chkIsActive").removeProp('checked', 'checked');
or,
$("#chkIsActive").prop('checked', false);
Now I Will give you an example of CheckBox functionality. I think it will be helpfull.
----------UI-------------------
Create an Input Item in html :
<div>
<ul>
<li>
<label for="IsActive" class="lbl widthSize20_per">Is Active:</label>
<input type="checkbox" id="chkIsActive" name="IsActive" />
</li>
</ul>
</div>
----------Javascript-------------------
How to get value from CheckBox:
var IsActive = $("#chkIsActive").is(":checked") == true ? 1 : 0;
----Fill Checkbox-------------
var data = GetDataFromServer();
if (data.IsActive == 1) {
$("#chkIsActive").prop('checked', 'checked');
} else {
$("#chkIsActive").removeProp('checked', 'checked');
}
--------is checked event------------------
CheckBox click event:
$('#chkIsActive').live('click', function (e) {
var $cb = $(this);
if ($cb.is(":checked")) {
//do something
} else {
//do something
}
});
Clear CheckBox:
---------------------------------------------------------
$("#chkIsActive").removeProp('checked', 'checked');
or,
$("#chkIsActive").prop('checked', false);
0 Comments