Javascript - 如何使用确认消息取消发布表单?

时间:2011-11-08 13:38:06

标签: javascript post velocity xwiki

以下是接收POST的代码:

{{velocity}}
#if ($request.getMethod() == "POST")
$request.getParameter("servicename")
#end
{{/velocity}}

以下是发送POST的代码:

{{velocity}}

{{html}}

以下是javascript的代码:

<script type="text/javascript">
function validateForm()
{

c = confirm("Are you sure ?");
if(c == true){
alert("The foo has been created");
}else{
alert("The foo creation has been canceled.");
return false;
}
}
</script>

以下是html表单的代码:

<form name="myForm" action="$doc.getURL('view')" method="post" onsubmit="return  validateForm()">
<input type="hidden" name="xaction" value="createservice" />
 Foo Name:
<input type="text" name="servicename" />
 <input type="submit" value="Create" />
 </form>
{{/html}}

 {{/velocity}}

1 个答案:

答案 0 :(得分:1)

我不知道这是否会对您有所帮助,但以下代码有效:

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

function foo() {
    if (confirm("Are you sure?")) {
        alert("OK");
        return true;
    }
    else {
        alert("KO");
        return false;
    }
}

</script>
</head>
<body>

<form method="post" onsubmit="return foo()">
    <input type="text" />
    <input type="submit" value="Go" />
</form>

</body>
</html>

然后你可以摆脱foo

<form method="post" onsubmit="return confirm('Are you sure?')">