0

I have some checkboxes and want to pass their values (value=1) to a controller when they are checked, if not checked just leave it null.

<div  class="col-md-7">
    <div class="checkbox-list" style="padding-top: 8px;">
        <input name="add_content" id="add_content" value="1" type="checkbox" /> 
        <input name="edit_content" id= "edit_content" value="1" type="checkbox" /> 
    </div>
</div>
save: function () {
    var self = this;
    var id = $("#in_id").val();
    var add_content = $("[add_content='add_content']").val();
    var edit_content = $("[edit_content='edit_content']").val();

    KeyWord.updateUser(id, add_content, edit_content, delete_content, manage_user, view_log, function (data) {
        alert("data saved succesfully.");
    }, function (error) {
        jqXhrErr(error);
    });
}

var KeyWord = function () {
    var url_update = "api/user/Update";
    return {
        updateUser: function (id, add_content, edit_content, done, fail, always)  {
            var jqxhr = $.post(url_update, { 
                id: id, 
                add_content: add_content, 
                edit_content: edit_content
            });
            return jqXhrHandler(jqxhr, done, fail, always);
        }
    }
}();

The problem is that the controller only gets non-checkbox value but it does not receive checkbox value when they check?

Any error here?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Saif
  • 2,611
  • 3
  • 17
  • 37
  • read this http://stackoverflow.com/questions/6169826/propchecked-false-or-removeattrchecked – Farhan Nov 26 '15 at 16:19
  • do you want to add trigger on when checkbox clicked? – Farhan Nov 26 '15 at 16:23
  • no, just pass its value to controller.Other input values such as text send to controller successfully and only check box did not work for me – Saif Nov 26 '15 at 16:25
  • try to replace $("[edit_content='edit_content']").val(); with $("#add_content").val(); same for edit – Farhan Nov 26 '15 at 16:27

1 Answers1

0

replace following code

var add_content = $("[add_content='add_content']").val();
var edit_content = $("[edit_content='edit_content']").val();

with

var add_content = 0;
var edit_content = 0;
if($("#add_content").is(":checked")){
add_content = 1;

}
if($("#edit_content").is(":checked")){
edit_content = 1;
}
Farhan
  • 1,453
  • 2
  • 15
  • 20