Naughty Ninja Naughty Ninja - 1 year ago 77
Javascript Question

Ajax call passing always true for checkbox data MVC/C#

Ajax call is always passing true for isHalfDay checkbox, every other value that is being passed is correct apart from this one. What might be the reason?

UpdateDays: function () {
url: $("#numberOfDays").data("url"),
data: {
startDate: $("input[name=StartDate]").val(),
endDate: $("input[name=EndDate]").val(),
employeeId: $("#holiday-editor").data("employee"),
isHalfDay: $("input[name=HalfDay]").val()
method: "POST"
}).success(function (response) {

Answer Source

It's because the checkbox always has a value. If you want to know if it's checked or not use prop('checked'), like this:

isHalfDay: $("input[name=HalfDay]").prop('checked')

Also note that there is no success() method on the promise returned from the $.ajax call - I think you mean done() instead.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download