0

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?

Jay
  • 3,012
  • 14
  • 48
  • 99

2 Answers2

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

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

     });
 }
Torxed
  • 22,866
  • 14
  • 82
  • 131
Ramki
  • 166
  • 1
  • 12
  • Also be aware of `$(this)` vs `this`: http://stackoverflow.com/questions/1051782/jquery-this-vs-this – Torxed Aug 12 '13 at 13:40
0

Try this

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