checkbox - checked or unchecked with jquery and mysql

前端 未结 5 1468
清歌不尽
清歌不尽 2020-12-06 11:52

I am currently doing a system where it has to be possible to check/uncheck a checkbox. Everytime it changes status I need jquery to make and ajax call to a page, that update

相关标签:
5条回答
  • 2020-12-06 12:07

    Which bit are you stuck on? You should probably have something like this...

    $('#myCheckbox').click(function() {
        var checked = $(this).is(':checked');
    
        $.ajax({
            type: "POST",
            url: myUrl,
            data: { checked : checked },
            success: function(data) {
                alert('it worked');
            },
            error: function() {
                alert('it broke');
            },
            complete: function() {
                alert('it completed');
            }
        });
    });
    
    0 讨论(0)
  • 2020-12-06 12:09

    Detect if checkbox is checked:

    if ( $('#id').is(':checked') ) { }
    

    This can be executed in a function that is triggered by "onchange" event.

    function checkCheckboxState() {
    
        if ( $('#id').is(':checked') ) { 
    
            // execute AJAX request here
    
        }
    }
    
    0 讨论(0)
  • 2020-12-06 12:15

    For example you can do it like this:

    First you have to look if the checkbox is checked:

    $("#yourSelector").live("click", function(){
            var id = parseInt($(this).val(), 10);
            if($(this).is(":checked")) {
                // checkbox is checked -> do something
            } else {
                // checkbox is not checked -> do something different
            }
    });
    

    You can load specific content via Ajax:

    $.ajax({
                    type: "POST",
                    dataType: "xml",
                    url: "path/to/file.php",
                    data: "function=loadContent&id=" + id,
                    success: function(xml) {
                        // success function is called when data came back
                        // for example: get your content and display it on your site
                    }
    });
    
    0 讨论(0)
  • 2020-12-06 12:21
    <input type="checkbox" name="foo" value="bar" class="checkIt"/>
    
    <script type="text/javascript">
        $('.checkIt').bind('click', function() {
            if($(this).is(":checked")) {
                // checkbox is checked
            } else {
                // checkbox is not checked
            }
        });
    </script>
    

    You can now have more than one checkbox.

    0 讨论(0)
  • 2020-12-06 12:30

    Something like this probably?

    $('.checkbox').click(function (){
        var val = $(this).is(':checked');
        $.load('url_here',{status:val});
    });
    
    0 讨论(0)
提交回复
热议问题