I am using Bootstrap Datepicker and I want to submit a form with the new date value when the new date is selected - so no submit button.
My form HTML is:
<form id="date-form" method="post" action="" accept-charset="utf-8" >
<input data-date-autoclose="true" data-date-start-view="months" data-date-format="M yyyy" data-date-min-view-mode="months" type="text" id="date-input" />
I have this posting the form OK:
$('#input-date')
.datepicker()
.on('changeDate', function(){
$('#date-form').submit();
});
However I need to get the new date value so have this:
$('#input-date')
.datepicker()
.on('changeDate', function(e){
$('#date-form').submit(function() {
var date = $('#input-date').val();
$.post("my-page", function() {
alert("success with posting " + date);
});
// even if I just have console.log("worked"); in here it wont log
});
});
With the above code a POST request is not made at all. As I mention I can't write to the console either.
How can I get the form to post?
The first example you are submitting the form.
The second example you are assigning a function to the submit event. It is not going to submit the form. You would have to call .trigger("submit"); to execute it.
I think you just want
$('#input-date')
.datepicker()
.on('changeDate', function(e){
var date = $('#input-date').val();
$.post("my-page", function() {
alert("success with posting " + date);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With