$('#delete').submit(function(event){
if(!confirm("some text")){
event.preventDefault();
}
});
Answer from iHadaj on Stack Overflow $('#delete').submit(function(event){
if(!confirm("some text")){
event.preventDefault();
}
});
You are submitting the form and after are checking for confirm, you need to the inverse
JS:
$(function() {
$("#delete_button").click(function(){
if (confirm("Click OK to continue?")){
$('form#delete').submit();
}
});
});
HTML:
<form id="delete" action="{% url 'item_delete' item.id %}" method="post">{% csrf_token %}
<input id="delete_button" class="floatright" type="button" value="Delete" />
</form>
$('#myForm').submit(function() {
var c = confirm("Click OK to continue?");
return c; //you can just return c because it will be true or false
});
sample fiddle: http://jsfiddle.net/z68VD/
html:
<form id="uguu" action="http://google.ca">
<input type="submit" value="text 1" />
</form>
jquery:
$("#uguu").submit(function() {
if ($("input[type='submit']").val() == "text 1") {
alert("Please confirm if everything is correct");
$("input[type='submit']").val("text 2");
return false;
}
});
Submit Form ( POST ) after confirmation
[jQuery] Dialog confirmation before submit
Jquery Validate Plugin: A confirm alert on submit
[Resolved] Are you Sure? jQuery popup on Form Submit
Hmm... theres is a problem with the other answers on here: they don't work against your HTML.
There's a bug in jQuery (I assume it's a bug), where if an element on your form has aname of submit, then triggering the submit event of the form will not work.
You will need to remove the name attribute from your input type="submit" button or simply give it a name other than "submit".
HTML
<form action ="/submit-page/" method='post' class="editable">
<fieldset>
<select name="status" id='status'>
<option value="Submitted">Submitted</option>
<option value="Canceled">Canceled</option>
<option value="Application">Application</option>
</select>
<input type="submit" value="SAVE" name="submit-button"/>
</fieldset>
</form>
jQuery
$('#status').on('change', function() {
var $this = $(this),
val = $this.val();
if (val === 'Canceled' && confirm("are you sure?")) {
$this.closest('form').submit();
}
});
PHP
$submitted = !empty($_POST['submit-button']);
if($submitted)
{
// Submit button was pressed.
}
else
{
// Form was submitted via alternate trigger.
}
Example
Working: http://jsfiddle.net/xixonia/KW5jp/
Not Working: http://jsfiddle.net/xixonia/KW5jp/1/
Edit
You have since updated your question, and this answer is no longer a valid solution for what you are looking for. Instead, look at Chris Platt's answer.
Edit Again
This is a modified version of Chris Platt's answer. It simply waits until the DOM is ready (elements are loaded) before it executes the logic contained within the first $(...).
$(function() { // this first jQuery object ensures that...
/// ... the code inside executes *after* the DOM is ready.
$('form.editable').submit(function(){
if ($('#status').val()=='Canceled') {
if (!confirm('Warning message here. Continue?')) {
return false;
}
}
});
});
$('form.editable').submit(function(){
if ($('#status').val()=='Canceled') {
if (!confirm('Warning message here. Continue?')) {
return false;
}
}
});