开发者

jQuery: changing on mark checkbox

开发者 https://www.devze.com 2023-02-06 12:43 出处:网络
When you mark/unmark the ch开发者_运维技巧eckbox I want it to window.location = index.php?mark=1 if it\'s marked, and if you click and it gets unmarked it should be mark=0$(\'input:checkbox\').click(f

When you mark/unmark the ch开发者_运维技巧eckbox I want it to window.location = index.php?mark=1 if it's marked, and if you click and it gets unmarked it should be mark=0


$('input:checkbox').click(function(){
  window.location='/index.php?mark=' + ($(this).attr('checked') ? 1 : 0);
});

Should do the trick, but note that means it's going to cause a page reload between checks.

EDIT or as William mentioned, you can bind to .change() as well.

EDITv2 Working example: http://www.jsfiddle.net/T5CaC/


You're looking for jQuery.change(). When the checkbox check if the checkbox is checked or not.


$('#someCheckbox').change(function() {
    window.location.search = ("?mark=" + (+this.checked));
});


Something like this:

$('input#mycheckbox').change(function () {
    if ($(this).attr("checked")) {
        window.location = index.php?mark=1; 
        return;
    }
    window.location = index.php?mark=0;
});

Check Here

0

精彩评论

暂无评论...
验证码 换一张
取 消