I am trying to create an alert, to insure that the user is submitting the correct information and if 'Ok' is clicked rather than cancel, the link is clicked and <a> sent. I have nearly achieved it, the alert activates, however does not activate if ok is clicked. Unfortunately, I am not a js wizard, yet..
EDIT :
click => preventDefault => alert (yes, no) if yes(send) if no dont.
<script>
jQuery(document).ready(function($){
$("input.btn-default").click(function(e){
e.preventDefault();
var answer=confirm('Are you sure that you want to do this?');
if(answer == true){
///not sure how to remove the prevent default and send link?!
}
else{
return;
}
});
});
</script>
Try the following code using return false; if the user cancel confirm dialog and window.open( $(this).att('href') ); to open the link when he user click OK :
$("input.btn-default").click(function(e)
{
e.preventDefault();
if( confirm('Are you sure that you want to do this?') )
{
window.open( $(this).att('href') );
}
else
{
return false;
}
});
Hope this helps.
confirm() is modal, so you just need to check for answer:
jQuery(document).ready(function ($) {
$("input.btn-default").click(function (e) {
var answer = confirm('Are you sure that you want to do this?');
if (!answer) {
e.preventDefault();
}
});
});
Related
I want to check if there's at least one checkbox that checked in a form after the submit button is clicked, if no, the the user should get message and the form SHOULDN'T be submit, so I wrote this code:
jQuery(document).ready(function ($) {
$('#price_quote_create_invoice').click(function () {
$('.price-quotes-table input[type="checkbox"]').each(function () {
if ( $(this).is(':checked') ){
$('#post').submit();
return false;
} else {
alert("You didn't chose any checkbox!");
return false;
}
}) ;
});
});
However, after I press ok on the alert box, the form does submit.
Any idea why this is happening?
Your code is almost right.
You return false on a click event but what you really want to do is to stop the submit event.
Change like this.
$('#yourFormId').submit(function(){
if (!$(this).find('input[type="checkbox"]:checked').length) {
alert("You didn't chose any checkbox!");
return false;
}
});
alert('You didn't chose any checkbox!');
You have an additional comma in your alert..
You can use event.preventDefault() to do this. event is the first argument of your click function. Reference
When a submit button is pushed, I want to give the user an alert to make sure they want to delete something.
So far, I have this to do so
In the Twig file
{{ form_start(form, {'attr': {'id': 'delete_form'}}) }}
And in the Javascript file
window.onload = function() {
confirmDelete();
};
function confirmDelete(){
var el = document.getElementById('delete_form');
if (el) {
el.addEventListener('submit', function () {
return confirm('Are you sure you want to delete this question?');
}, false);
}
else {
console.log("No form found");
}}
But now, when the cancel button of the alert is clicked, the data is still being deleted.
What am I doing wrong?
You are not preventing the form from being submited.
And in your confirm delete you will have to trigger submit event if the user clicks yes else do nothing.
// listen to the submit event
$('#delete_form').on('submit', function(e) {
// prevent form from being submitted
e.preventDefault();
confirmDelete();
});
function confirmDelete() {
var result = confirm('Are you sure you want to delete this question?');
// I do not know what result returns but in case that yes is true
if (result === true) {
$('#delete_form').submit();
}
}
I want to make a confirmation before user leaving the page if there's a incomplete registration or refresh the registration page
JQUERY CODE:
$(document).ready(function(){
$('input[name=subdomain]').keyup(subdomain_check);
$('input[name=password]').keyup(password_strenght);
$('input[name=c_password]').keyup(password_check);
$('input[name=email]').keyup(email_check);
$("#install").on('submit', function(){
return subdomain_check() && password_strenght() && password_check() && email_check();
});
window.onbeforeunload = function() {
return 'Are you sure you want to navigate away from this page?';
};
});
But the confirmation pop up in every step of the registration, even if the registration is complete! How can I prevent that from happening?
You need to put this code in js file,
/* Dirty Flag */
$(document).ready(function() {
is_dirty = false;
$(window).bind('beforeunload', handle_unload_event);
setDirtyFlag("form1");// Here form1 is the ID of form
});
function setDirtyFlag(frmID){
$('#'+frmID).find(':input').each(function(){
$(this).change(function(){
is_dirty = true;
});
});
}
I ask the user "Are you sure?" when clicking on a form submit button:
{{ Form::open(array('url' => 'myhome/pic-remove', 'onsubmit' => 'return confirm(\'Are you sure?\');')) }}
And I deactivate the submit button on click:
$('#submitbutton').click(function() {
$('#submitbutton').hide();
$('#isloading').show();
});
The Problem is, the submit button is also deactivated, if the user 'is not sure' and clicks 'cancel'.
How can I deactivate the submit button only if user 'is sure' on the first question and clicks 'ok' ?
edit:
ok i have it like this now, seems to work
$('#submitbuttonconfirm').click(function() {
var r = confirm('Are you sure?');
if (r == true)
{
$('#submitbuttonconfirm').hide();
$('#isloading').show();
return true;
}
else
{
return false;
}
});
I think the problem is, that the click event is fired before the submit event, so you hide the submit button before you even ask the user.
You can use jQuery to handle submit event instead of of using click and disable the button there:
{{ Form::open(array('url' => 'myhome/pic-remove')) }}
$('form').submit(function(e) { // Use the id of the form here, if you have more than one
if(confirm('Are you sure?')) {
$('#submitbutton').hide();
$('#isloading').show();
} else {
return false;
}
});
I assume the top section is you sending the JavaScript to be executed from the server to the client. I'm not sure why you're doing that instead of writing it as JavaScript directly but I suppose it doesn't matter. As Givi said, get the response from the confirm dialog and then use it as a decision point. Since I'm not sure what this is, I'm only guessing as to this code but it should give you an idea.
{
{ Form::open(
array(
'url' => 'myhome/pic-remove',
'onsubmit' => 'if(confirm(\'Are you sure?\')){ $(\'#submitbutton\').hide(); $(\'#isloading\').show(); return true; } return false; '
)
) }
}
I have a problem with the JS's confirm in my PartialView. The code below works fine if the page is NOT a PartialView, but, if the page IS a PartialView, if I click OK on that confirm dialog it does not close. Why ?
$("input.del-file-cb").live('click',function () {
if ($(this).is(':checked')) {
var ans = confirm('Are You sure ?');
if (ans) {
....
} else {
$(this).attr("checked", false);
}
}
});
Edit
It looks that it is a double confirmation... like here jQuery/Javascript confirm coming up twice
The solution:
$("input.del-file-cb").live('click',function (e) {
if ($(this).is(':checked')) {
e.preventDefault();
... the rest of the code ...