admin管理员组

文章数量:1384122

Here is code:

<form  method="post" id="cp_ind_form">

    // lots of input fields!
    <input type="submit" name="update_submit" value="Update"  />
    <input type="submit" name="delete_submit" value="Delete"  onclick="deleteConfirm()" />

</form>

scripts.js (confirmed that this file is connected to above page)

(function deleteConfirm() {
    var s = document.getElementById('confirm');
    s.onchange = function() {
        var yes = confirm('Do you really want to delete this location?');
        if (yes) { 
            var f = document.getElementById('cp_ind_form');
            f.submit();
        }
    }
})();

}

This is driving me insane. Such a basic function is not working here? I am basically copying it from other code that I have that DOES work, and this is no different. Can someone confirm if I am missing a small detail?

Here is code:

<form  method="post" id="cp_ind_form">

    // lots of input fields!
    <input type="submit" name="update_submit" value="Update"  />
    <input type="submit" name="delete_submit" value="Delete"  onclick="deleteConfirm()" />

</form>

scripts.js (confirmed that this file is connected to above page)

(function deleteConfirm() {
    var s = document.getElementById('confirm');
    s.onchange = function() {
        var yes = confirm('Do you really want to delete this location?');
        if (yes) { 
            var f = document.getElementById('cp_ind_form');
            f.submit();
        }
    }
})();

}

This is driving me insane. Such a basic function is not working here? I am basically copying it from other code that I have that DOES work, and this is no different. Can someone confirm if I am missing a small detail?

Share Improve this question edited Jan 25, 2013 at 16:36 JaredMcAteer 22.6k5 gold badges52 silver badges66 bronze badges asked Jan 25, 2013 at 16:33 TheLettuceMasterTheLettuceMaster 15.7k50 gold badges158 silver badges266 bronze badges 4
  • 1 Have you looked at the error console? There should be a syntax error, your brackets don't match – Pekka Commented Jan 25, 2013 at 16:34
  • What errors do you receive when you submit? – Kyle Commented Jan 25, 2013 at 16:34
  • look at the bracket in the scripts.js . maybe there is a } on the wrong position. Which elment has the id "confirm" ? – Mic Commented Jan 25, 2013 at 16:36
  • I do not receive any error. I am using Aptana IDE. Don't really use it with JS much so didn't think to look for errors. – TheLettuceMaster Commented Jan 25, 2013 at 16:41
Add a ment  | 

3 Answers 3

Reset to default 5

Look at the code.

You are adding an onchange event to something when you click the submit.

You are not triggering the confirm to be shown, and you are not cancelling the original submission.

AND it makes no sense to have it wrapped with (funciton(){})();

It should be

function deleteConfirm() {
    return confirm('Do you really want to delete this location?');   
}

and

onclick="return deleteConfirm();"

To actually execute the confirm when deleteConfirm is called, change your code to this:

function deleteConfirm() {
    var yes = confirm('Do you really want to delete this location?');
    if (yes) { 
        document.getElementById('cp_ind_form').submit();
    }
    // block default submit
    return(false);
}

This also returns false so that the default form submission doesn't happen.

Then, change your HTML to this:

onsubmit="return deleteConfirm()"

If you handle the onsubmit instead of onclick, you can block the default submit by returning false from the handler.

deleteConfirm() is run on button click. Inside this function you are assigning code to the eventonchange which than waits for the item #confirm to be changed. I don't know where is that element but I pressume the event is never fired so the confirm box never shows

本文标签: htmlJavascript Confirm Dialog Not ActivitingStack Overflow