如何通过ajax

时间:2016-07-25 10:22:46

标签: javascript jquery html ajax

我想使用ajax将复选框的值发送到数据库。通过在互联网上的一些搜索,我设法得到这么远。这是我一直在使用。我需要对json2.stringify进行哪些更改才能正确发送我的值。

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);

1 个答案:

答案 0 :(得分:1)

使用.prop('checked')属性

<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);