最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to add confirm box before submit buttons? - Stack Overflow

programmeradmin2浏览0评论

adding confirm box

Here is my code and I want to add a confirmation box to it.

I have a form with two submit buttons. I need the confirmation box with them and then send to the next page. How can I do this? this is the code for confirmation box,how can i place it in my form ?

onclick="return confirm('Are you sure?');"

myform.jsp

<form id="form1" name="form1" method="post" action="">
    <p>&nbsp;</p>
    <p align="center">Enter your choice:-
    <label> <br />
    <label>
       <input type="submit" name="Submit" value="delete" onclick="this.form.action='logi.jsp?flag=1&act=1';this.form.submit();" />
    </label>
    <br />
    </br>
    <label>
        <input type="submit" name="Submit" value="Delete" onclick="this.form.action='delete1.jsp?flag=1&act=1';this.form.submit();" />
    </label>
    <br/>
    </div>
</form>

adding confirm box

Here is my code and I want to add a confirmation box to it.

I have a form with two submit buttons. I need the confirmation box with them and then send to the next page. How can I do this? this is the code for confirmation box,how can i place it in my form ?

onclick="return confirm('Are you sure?');"

myform.jsp

<form id="form1" name="form1" method="post" action="">
    <p>&nbsp;</p>
    <p align="center">Enter your choice:-
    <label> <br />
    <label>
       <input type="submit" name="Submit" value="delete" onclick="this.form.action='logi.jsp?flag=1&act=1';this.form.submit();" />
    </label>
    <br />
    </br>
    <label>
        <input type="submit" name="Submit" value="Delete" onclick="this.form.action='delete1.jsp?flag=1&act=1';this.form.submit();" />
    </label>
    <br/>
    </div>
</form>
Share Improve this question edited Jun 11, 2013 at 16:57 Pradeep Pati 5,9293 gold badges30 silver badges43 bronze badges asked Jan 8, 2013 at 9:24 user1950705user1950705 411 gold badge2 silver badges6 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

On button class try this

$('.class').appendTo('body')
  .html('<div><h6>Yes or No?</h6></div>')
  .dialog({
  modal: true, title: 'message', zIndex: 10000, autoOpen: true,
  width: 'auto', resizable: false,
  buttons: {
      Yes: function () {
          doFunctionForYes();
          $(this).dialog("close");
      },
      No: function () {
          doFunctionForNo();
          $(this).dialog("close");
      }
  },
  close: function (event, ui) {
      $(this).remove();
  }
});

try this

<form id="form1" name="form1" method="post" action="/your/url.xyz" onsubmit="return confirm('bla bla');">
<input type="submit" name="Submit" value="Delete" onclick='confirm(this,"delete1.jsp?flag=1&act=1")' />
<input type="submit" name="Submit" value="delete" onclick='confirm(this,"logi.jsp?flag=1&act=1")' />

function confirm(this,foo1)
{
var foo = confirm('Are you sure?');
if (foo) {this.form.action=foo1;this.form.submit();}
else {alert('Try Later');}
}

The following snippet is the solution. You need to remove this.form.submit() as the buttons are submit buttons and by default they submit.

Just add onsubmit=" return window.confirm('Are you sure?');" to the form and remove this.form.submit() from submit buttons.

<form id="form1" name="form1" method="post" action="" onsubmit=" return window.confirm('Are you sure?');">
                        <p>&nbsp;</p>
                        <p align="center">Enter your choice:-
                            <label> <br />


         <label>
           <input type="submit" name="Submit" value="delete" onclick="this.form.action='logi.jsp?flag=1&act=1';" />
            </label>
                                <br />

                            </br>
                   <label>
                 <input type="submit" name="Submit" value="Delete" onclick="this.form.action='delete1.jsp?flag=1&act=1';" />
           </label>
                                <br/>

                        </div></form>
See this code confirm box works in this way and one sugesstion your code need a lot pf improvement

<html>
<head>
<script>
function disp_confirm()
{
var r=confirm("Press a button!")
if (r==true)
  {
  alert("You pressed OK!")
  }
else
  {
  alert("You pressed Cancel!")
  }
}
</script>
</head>
<body>

<input type="button" onclick="disp_confirm()" value="Display a confirm box">

</body>
</html> 
发布评论

评论列表(0)

  1. 暂无评论