如何做一个确认弹出到提交按钮然后发送请求?

时间:2022-05-19 22:10:23

I am learning web development using Django and have some problems in where to put the code taking chage of whether to submit the request in the HTML code.

我正在学习使用Django进行web开发,并且在如何在HTML代码中提交请求时遇到了一些问题。

Eg. There is webpage containing a form(a blog) to be filled by the user, and upon click on the Save button,there is a pop up asking whether you want to confirm or not. If click on confirm, then the request is sent.

如。有一个包含用户要填写的表单(博客)的网页,点击Save按钮,会弹出一个询问是否需要确认的弹出窗口。如果单击confirm,则发送请求。

I searched and find this javascript code.

我搜索并找到了这个javascript代码。

<script type="text/javascript">
function clicked() {
    alert('clicked');
}

<input type="submit" onclick="clicked();" value="Button" />

But I guess this is not the correct function as it seems to me that whenever you click on the Button, the request will be submitted. So How can I delay the submit request until user has confirm the submit?

但我想这并不是正确的函数,因为在我看来,只要你点击按钮,请求就会被提交。那么,我如何将提交请求延迟到用户确认提交之后呢?

4 个解决方案

#1


22  

I believe you want to use confirm()

我想你想使用confirm()

<script type="text/javascript">
    function clicked() {
       if (confirm('Do you want to submit?')) {
           yourformelement.submit();
       } else {
           return false;
       }
    }

#2


95  

The most compact version:

最紧凑的版本:

<input type="submit" onclick="return confirm('Are you sure?')" />

The key thing to note is the return

要注意的关键是返回

-

- - - - - -

Because there are many ways to skin a cat, here is another alternate method:

因为有很多方法可以剥猫皮,这里有另一种方法:

HTML:

HTML:

<input type="submit" onclick="clicked(event)" />

Javascript:

Javascript:

<script>
function clicked(e)
{
    if(!confirm('Are you sure?'))e.preventDefault();
}
</script>

#3


5  

Use window.confirm() instead of window.alert().

使用window.confirm()而不是window.alert()。

HTML:

HTML:

<input type="submit" onclick="return clicked();" value="Button" />

JavaScript:

JavaScript:

function clicked() {
    return confirm('clicked');
}

#4


3  

<script type='text/javascript'>

function foo() {


var user_choice = window.confirm('Would you like to continue?');


if(user_choice==true) {


window.location='your url';  // you can also use element.submit() if your input type='submit' 


} else {


return false;


}
}

</script>

<input type="button" onClick="foo()" value="save">

#1


22  

I believe you want to use confirm()

我想你想使用confirm()

<script type="text/javascript">
    function clicked() {
       if (confirm('Do you want to submit?')) {
           yourformelement.submit();
       } else {
           return false;
       }
    }

#2


95  

The most compact version:

最紧凑的版本:

<input type="submit" onclick="return confirm('Are you sure?')" />

The key thing to note is the return

要注意的关键是返回

-

- - - - - -

Because there are many ways to skin a cat, here is another alternate method:

因为有很多方法可以剥猫皮,这里有另一种方法:

HTML:

HTML:

<input type="submit" onclick="clicked(event)" />

Javascript:

Javascript:

<script>
function clicked(e)
{
    if(!confirm('Are you sure?'))e.preventDefault();
}
</script>

#3


5  

Use window.confirm() instead of window.alert().

使用window.confirm()而不是window.alert()。

HTML:

HTML:

<input type="submit" onclick="return clicked();" value="Button" />

JavaScript:

JavaScript:

function clicked() {
    return confirm('clicked');
}

#4


3  

<script type='text/javascript'>

function foo() {


var user_choice = window.confirm('Would you like to continue?');


if(user_choice==true) {


window.location='your url';  // you can also use element.submit() if your input type='submit' 


} else {


return false;


}
}

</script>

<input type="button" onClick="foo()" value="save">