Call Ajax or submit the button when checkbox is checked
Remove the submit event, use a change event, get all the checked sids
$('.supplierClass').change(function() {
if( $('.supplierClass:checked').length){
var sids = [];
$('.supplierClass:checked').each(function(i,v) {//get all the sids of the elements checked
sids.push($(v).val());
});
$.ajax({
url : 'process/get-vehicle.php',
type : 'POST',
dataType : 'html',
data : {
sid : sids,
},
beforeSend : function () {
$('.allVehicle').html('Please wait...');
},
success : function ( result ) {
$('.allVehicle').html(result);
}
});
}
});
Using AJAX to post checkbox list without submit button
Instead of var data = $("form1").serialize();
you should use var data = $("[name='form1']").serialize()
.
Using a plain string like that won't find you anything. You must specify that you want the element with the name, 'form1'
Ajax Checkbox that submit with prevent click upon post/submit
You can disable the checkbox before starting the ajax call. You may use the prop()
method to do that. Set the disabled
property value to true
$(".chkOverride").click(function (e) {
var _this=$(this);
_this.prop('disabled', true);
var userId = $("#UserId").val();
var isChecked = $(this).is(":checked")
$.ajax({
url: "/Worker/Worker?Id=" + Id + "&isChecked=" +
isChecked + "&UserId=" + UserId,
type: "post",
success: function (result) {
alert("Success!");
//No point in enabling as you are going to reload the page
_this.prop('disabled', false);
location.reload();
},
error: function () {
alert("Error :(");
_this.prop('disabled', false);
}
});
});
Disable button if checkboxes are unchecked - across groups - in an ajax response
A for loop (or forEach) would be appropriate.
Additionally make sure you disable the 'submit' if either group is unselected (I use .some
below). Your current code will disable it if the last group is unselected.
var checkboxGroups = ['clone-ctp__filters', 'clone-ctp__users'].map(containerClass => $(`.${containerClass} input[type="checkbox"]`));
checkboxGroups.flat().forEach(checkbox =>
checkbox.change(() =>
$('button').prop('disabled', checkboxGroups.some(checkboxes => checkboxes.filter(':checked').length < 1))));
$('input[type="checkbox"]').change();
Or in vanilla JS:
const $ = document.querySelector.bind(document);const $$ = document.querySelectorAll.bind(document);
var checkboxGroups = ['clone-ctp__filters', 'clone-ctp__users'] .map(containerClass => [...$$(`.${containerClass} input[type="checkbox"]`)]); checkboxGroups.flat().forEach(checkbox => checkbox.addEventListener('change', () => $('button').disabled = checkboxGroups.some(checkboxes => checkboxes.every(checkbox => !checkbox.checked))));
$('button').disabled = true;
<div class="clone-ctp__filters"> <input type="checkbox"> <label>Foo 1</label> <input type="checkbox"> <label>Bar 2</label> <input type="checkbox"> <label>Baz 3</label></div>
<div class="clone-ctp__users"> <input type="checkbox"> <label>Foo 5</label> <input type="checkbox"> <label>Bar 6</label> <input type="checkbox"> <label>Baz 7</label></div>
<button>Continue</button>
How to read if a checkbox is checked in ajax?
Toggle value for ajax using ternary operator:
document.getElementById("ch").checked ? 'y' : 'n'
Submit a checkbox without refresh using JS/AJAX
First, let's remove the inline js from your html:
<td>
<form id="checkgo" method="post">
<input type="checkbox" name="wegeb" id="wegeb" value="<?php echo $row['bestellnr'] ?>">
</form>
<?php echo $row['we_gebucht']; ?>
</td>
Then we bind the ajax call to the right event:
$(function() {
$("#wegeb").change(function (e){
e.preventDefault(); //not necessary since we are binding to the checkbox
var wegeb = $(this).val();
var info = {wegeb: wegeb};
$.ajax({
type : "POST",
url : "addlist.php",
data : info,
});
});
});
This way every time you check the checkbox or uncheck it, it will update the database accordingly
Related Topics
Barcode Scanner for Mobile Phone for Website in Form
How to Change the State Correctly (Read-Only Error)
How to Create a Horizontal Scrolling Chart.Js Line Chart With a Locked Y Axis
Generate and Download CSV File With PHP and Ajax
Fix: Js Recursive Function to Get the Nested (Multilevel) Child Objects as Array of Objects
How to Get the Timezone Name in JavaScript
Copy/Paste from Excel to a Web Page
Check If Value Select2 Is Null
Node Server Running But Localhost Refusing to Connect
Trigger Click in Typescript - Property 'Click' Does Not Exist on Type 'Element'
Changing Svg Image Color With JavaScript
How to Fire an Event When V-Model Changes
Jquery Change Event Called Two Times
Detect If Page Is Load from Back Button
Javascript, Track Iframes Redirecting Top Window
Usestate Hook Not Updating a Value
How to Validate for Only CSV File Uploads Using the Pattern Attribute Using Html5