1

I have three checkboxes that who have checked/unchecked values populated from a model. I'm using an Ajax post on button click event to call controller actions for each checkbox changed event in order to update the DB.

Here is the code for one of the checkboxes (apart from the selector ID, they are all the same):

 $(document).ready(function () {

    //document.getElementById('UpdateButton').onclick = function () {
    $("UpdateButton").click = function () {
        $('#NatAm').change(function () {
            // if ($('#NatAm').is(':checked')) {
            $.ajax({
                //url: '@Url.Action("NativeUpdate", "Transactions")',
                    url: '/Transactions/NativeUpdate',
                    //data: { isNativeUp: true },

                    type: 'POST',
                    dataType: "json"
                });
                //}
            });

Edit (HTML/View Code):

 @Html.CheckBox("NatAm", (bool)@ViewBag.NativeAm)
 <input name="UpdateButton" id="UpdateButton" type="submit" value="Update" style="margin-left: 15px; margin-top: 3px;" class="btn btn-success" />

I cannot get this to work. Before adding the button, the ajax post was working fine. Thank you for your help!

5
  • Using $('#NatAm').change will attach change listener when click is triggered.. I doubt you need it like this.. Commented Mar 22, 2016 at 18:03
  • Add your HTML code please. Commented Mar 22, 2016 at 18:10
  • @ZakariaAcharki I added the html/view code Commented Mar 22, 2016 at 20:00
  • @RayonDabre Why do I need the change listener attached to the click event? Commented Mar 22, 2016 at 20:01
  • @ZakariaAcharki The id was unique. Am I confusing something? Commented Mar 22, 2016 at 20:02

3 Answers 3

3

Your click handler isn't right. You need to pass in the id of the button and use jQuery click handler. You also need not to nest the handlers:

$(document).ready(function() {
  $("#UpdateButton").click(update);
  $('#NatAm').change(update);
});

function update() {
  $.ajax({
    url: '/Transactions/NativeUpdate',
    type: 'POST',
    dataType: 'json'
  });
}

JSFiddle Demo: https://jsfiddle.net/vLzwuwdo/

Sign up to request clarification or add additional context in comments.

1 Comment

I appreciate your help. Nesting the handlers wasn't the right way to do it. However, your code fires the ajax post regardless if there was a change to the check box or not.
2

You're telling JQuery to look for 'UpdateButton' tags which in your case does not exist. You're missing the # which indicates an ID in your button.

Try this

$(document).ready(function () {

//document.getElementById('UpdateButton').onclick = function () {
$("#UpdateButton").click(function () {
    $('#NatAm').change(function () {
        // if ($('#NatAm').is(':checked')) {
        $.ajax({
            //url: '@Url.Action("NativeUpdate", "Transactions")',
                url: '/Transactions/NativeUpdate',
                //data: { isNativeUp: true },

                type: 'POST',
                dataType: "json"
            });
            //}
        }));

1 Comment

I did not have the selector id syntax correct, but that wasn't the major issue.
1
  1. id should be unique in same document (NatAm and UpdateButton), replace the duplicate ones by global classes will solve the first problem.

  2. You should not define event inside another since every time you trigger the first it will create new event for the element, in your case every time you click new change event will be created and attached to the first element with NatAm.

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.