Bibek Aryal Bibek Aryal - 4 months ago 16
jQuery Question

How to send checkbox value: either true or false, through ajax

I want to send the value of my checkbox to database using ajax. Through some searching in internet, I somehow managed to get this far.This is what I have been using. What changes do I need to make on json2.stringify for it to correctly send my values.

Html:

<input type="checkbox" id="txtCategoryIsPaid" name="cateogryIsPaid" value="Paid">Yes<br>


Javascript:

AddCategory: function () {
BusinessManagement.config.method = "AddBusinessCategory";
BusinessManagement.config.url = BusinessManagement.config.baseURL + BusinessManagement.config.method;
BusinessManagement.config.data = JSON2.stringify({

CategoryIsPaid: $('#txtCategoryIsPaid :checked').val(),

});
BusinessManagement.config.ajaxCallMode = 0;
BusinessManagement.ajaxCall(BusinessManagement.config);

Answer

Use .prop('checked') attribute

<HTML>
<input type="checkbox" id="txtCategoryIsPaid" name="cateogryIsPaid" value="Paid">Yes<br>

Javascript
AddCategory: function () {                
            BusinessManagement.config.method = "AddBusinessCategory";
            BusinessManagement.config.url = BusinessManagement.config.baseURL + BusinessManagement.config.method;
            BusinessManagement.config.data = JSON2.stringify({

                CategoryIsPaid: $('#txtCategoryIsPaid').prop('checked') == true ? "true": "false",

            });
            BusinessManagement.config.ajaxCallMode = 0;
            BusinessManagement.ajaxCall(BusinessManagement.config);