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" />
onchangeis more appropriate thanonclickfor 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.