简体   繁体   中英

two submit buttons, with confirmation pop up only on one submit button

I have two submit buttons in a form that Lets user Update/ Delete content. I want a confirm pop only if the user clicks Delete button.

<html>
<head>
<script type="text/javascript">

function confirmation() {
        // I need to know which submit button was pressed.
       if (value==Delete){
        var answer = confirm("Cancel?")
            if (answer){
                    return true;
            //Continue as intended
            }
            else{
             return false
            }
      }
}

</script>
</head>
<body>
<form id="Edit_Data">
//form input fields go here
<input name="action"  type="submit" onclick="confirmation()" value="Update">
<input name="action"  type="submit" onclick="confirmation()" value="Delete">
</form>
</body>
</html>

Any Ideas?

对于html标记中的onclick事件定义,为什么不调用单独的函数?

The simplest way would be to NOT call the javascript on the Update button.

If your form is static, then you do this in your IDE. If it's dynamic, then the dynamic code can create the form element accordingly.

If the form elements are generated automatically, then you should setup an event handler in JavaScript dynamically. Find all elements of type input with type attribute button or submit, and assign

elems[i].onclick = confirmation;

You'd then get the event object as a method parameter, and you could query that for the value of the button.

First of all you have several problems with your code. The value in your if statement is an undefined variable secondly you need to put quotes around the delete. Here is working fiddle http://jsfiddle.net/SMBWd/ and the relevant code change. Also, I would encourage you to look how to do this without using javascript in your HTML.

function confirmation(e) {
        // I need to know which submit button was pressed.
       if (e.value=='Delete'){

and in the HTML

<input name="action"  type="button" onclick="confirmation(this)" value="Update">
<input name="action"  type="button" onclick="confirmation(this)" value="Delete">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM