2

I am calling a function on onchange event of checkbox and it is working well but when I insert the ajax code in my function body,uncaught error function is undefined arises. Please take a look at my code so far:

input type="checkbox" onclick="OnChangeCheckbox (1)" id="myCheckbox1" value="22" />
    <label for="myCheckbox">Sample check box</label> 

and javascript function is:

function OnChangeCheckbox (checkbox) {
    var k = checkbox;
    if ($("#myCheckbox"+k.value).is(":checked")) {  
        $.ajax({
            async: false,
            url: 'getCDP.php', 
            data: { "size": vsize1},
            dataType: 'json',  
            success: function(data) {
                alert("ssdsd");
            }
        });
    }
    else {
        alert ("The check box is not checked.");
    }
}
3
  • wrong opening bracket - alert{"ssdsd"); should be alert("ssdsd"); Commented May 22, 2015 at 8:52
  • Why are you using inline event handlers when you have jQuery? They are not pretty and hard to maintain. Commented May 22, 2015 at 9:29
  • And onchange is more appropriate than onclick for checkboxes. So, do you have only one checkbox or multiple? If yes, do you want the ajax for only one checkbox or all or few? Give some more context to help us help you. Commented May 22, 2015 at 9:39

6 Answers 6

1

k.value is equal to checkbox.value; but the value of checkbox is 1, so k.value is undefined;

you can use:

if ($("#myCheckbox"+k).is(":checked")) {  
Sign up to request clarification or add additional context in comments.

Comments

1

As I said in my comments, do not use inline event handlers as they are a thing of the past and is a bad practice. Since you are using jQuery, make as much use of it as possible. Do not mix plain JS with jQuery unless you really have a need to do so. So, back to your question; your code may be re-written as the following.

The line "size": typeof vsize1 !== "undefined" && vsize1 || 0 is to ensure that the variable is defined and has a value or set it to 0 as a fallback if not.

From the doc: Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().

As per the above quote, I have commented the async: false since it shouldn't be used unless there is really a need to do so. If async: false needs to be used, you must use success (like in your question) in place of .done!

$(function() {
    //change event is more appropriate than click for form elements.
    $(".mycheck").on("change", function() {
        var $elem = $(this); // event target (one of the che
        var dataID = $elem.data("id"); // 1, 2 0r 3
        // Please note that the below line would only trigger the ajax call for the first checkbox as the data-id is being checked and when the box is checked.
        if ($elem.is(":checked") && dataID == 1) {
            $.ajax({
                //async: false,
                url: 'getCDP.php', 
                data: {
                    "size": typeof vsize1 !== "undefined" && vsize1 || 0
                },
                dataType: 'json'
            }).done(function(data) {
                alert("ssdsd");
            }).fail(function() {
                alert ("fail");
            });
        } else {
           alert ("The check box is not checked."); 
        }
    });
});

The above would work for just one checkbox and also for multiple. The associated HTML for the above code would look like this:

<input type="checkbox" id="myCheckbox1" value="22" class="mycheck" data-id="1" />
<input type="checkbox" id="myCheckbox2" value="23" class="mycheck" data-id="2" />
<input type="checkbox" id="myCheckbox3" value="24" class="mycheck" data-id="3" />

Comments

0

Your alert is wrong. You opened it with '{' rather than '('.

Works fine for me here: jsfiddle.net/adamjld/8wf6f9m8

Comments

0

You have few typo mistakes in your code.. read it carefully before you post question here..

  • missing ; after vsize1

  • wrong bracket in alert{"ssdsd");

Comments

0

This is beacause you did't defined

'vsize1' 

that you are passing as data. try defining it or give a static value here.

Comments

0

Check this fiddle please (ajax working).

You can define your checkbox like this:

<input type="checkbox" onchange="OnChangeCheckbox (this,1)" id="myCheckbox1" value="22" />
<label for="myCheckbox">Sample check box</label> 

Send the checkbox (using this) as parameter to the function

Define your vsize1 variable when the function is called or send it as a parameter (if it is depending on the checkbox that is checked)

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.