简体   繁体   中英

Checking whether checkbox has been unchecked

I am trying to send post requests to the database dependant on whether a checkbox is checked or unchecked I want to add to database or delete from database with the post requests. I have managed to successfully get the add working via post request but I am unable to get it to delete using the jQuery below:

$(function () {
    $("input:checkbox").change(function () {
        var fullURL = document.URL;
        var url = fullURL.split('userID=');
        var userID = url[1];
        var courseID = this.value;
        var postData = { 'userId': userID, 'courseID': courseID };
        if (!this.checked) {
            $.post('/Admin/UnenrolUser/', postData, function (data) {

            });
        }
        if (this.checked) {
            $.post('/Admin/EnrolUser/', postData, function (data) {

            });
        }


    });
});

My this.checked if statement seems to be working as expected. How can I get the unchecked to work in this context?

 if (this.checked) {
     $.post('/Admin/EnrolUser/', postData, function (data) {

     });
 } else {
     $.post('/Admin/UnenrolUser/', postData, function (data) {

     });
 }

尝试这个

if(!$(this).is(':checked'))

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM