I have written a if
condition for checking a checkbox:
$('input[type="checkbox"]').click(function() {
if (this.id == "internships" && this.checked) {
window.location.href = "<?php echo Yii::app()->createAbsoluteUrl('job/internships'); ?>";
}
else {
window.location.href = "<?php echo Yii::app()->createAbsoluteUrl('job/internships'); ?>";
}
});
Now I want to make sure that even after page refresh the checkbox does not change its state unless I/user do so. How can I do that?
You might just want to generate the checkbox with the checked
property already set. Following your code style, it would be something like
<?php
// prepare a string to be inserted.
// it will be either empty, or a nonempty "checked" attribute
$shouldBeChecked = is_this_the_internship_page() ? 'checked="checked"' : '';
?>
And then...
<input type="checkbox" <?php echo $shouldBeChecked; ?> >
if I remember PHP correctly :-)
See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox
Also, your JS contains two identical redirections at the moment of writing this answer.
Make use of localStorage
JS
$('#check').on('click', function(){
if(localStorage.getItem("checkedbox") == 'true') {
$(this).prop(checked, 'false');
localStorage.setItem("checkedbox", 'false');
}
else {
$(this).prop(checked, 'true');
localStorage.setItem("checkedbox", 'true');
}
})
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type = "checkbox" id="check"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.