Reputation: 5
When i normally use a confirm box for a link i do it this way:
<button type="button" class="btn btn-danger btn-sm" onclick="return confirm('Wollen Sie den Datensatz wirklich löschen?')">Delete</button>
Now i have to submit a form. In the message of the confirm box the value of an input field of the form has to be shown.
My attempt to solve this was:
<input id="endetzum" type="text" class="form-control" name="Kursende" form="formularmodal3">
<input type="submit" class="btn btn-primary" value="Auswählen" form="formularmodal3" id="buttonkursende">
$('#buttonkursende').click(function(){
var datumende = ($("#endetzum").val());
confirm('Der Kurs endet zum '+datumende);
});
It also works optically, but the form is always executed regardless of whether "ok" or "cancel" is clicked.
How can I prevent the form from being sent when "cancel" is clicked?
Upvotes: 0
Views: 723
Reputation: 1270
To avoid further execution with jQuery event binding, either the event function has to return false
or the event.preventDefault()
can be used
$('#buttonkursende').click(function(){
var datumende = ($("#endetzum").val());
return confirm('Der Kurs endet zum '+datumende);
});
// or
$('#buttonkursende').click(function(e){
var datumende = ($("#endetzum").val());
if(!confirm('Der Kurs endet zum '+datumende)) {
e.preventDefault();
}
});
Upvotes: 1
Reputation: 3261
to solve this issue use .submit()
event to handle this form submission code.
try below code it will work for you.
before using the below code please make sure formularmodal3 is the id of your form.
$('#formularmodal3').submit(function(){
var datumende = ($("#endetzum").val());
var result = confirm('Der Kurs endet zum '+datumende);
return result;
});
Upvotes: 0