我正在编写一个Perl脚本,它可以动态地打印出HTML页面。
该页允许用户通过单击表单上的单选按钮从四个任务中选择一个。
如果用户单击任务"Clear the signup sheet“的单选按钮,我应该向用户提供一个警告。如果用户单击OK,表单将提交。如果用户单击cancel,则不会发生任何事情。
我刚接触JavaScript,所以我根据老师的两个示例编写代码:一个是confirm方法在按钮上运行onClick,另一个是alert方法在表单上运行onSubmit。
但显然我不明白我在做什么,因为它不起作用。该对话框甚至不会弹出。
我该如何做我需要做的事情呢?
我的代码的相关部分(没有Perl print语句)目前看起来如下所示:
<script type="text/javascript">
function confirmation( ) {
if (maint.mainttask.value == "clearsheet") {
var answer = confirm('Are you sure you want to clear all entries?');
if (answer==true){
return(true);
} else {
return(false);
}
}
return(true);
}
</script>
<form method='post' action='path removed' name=maint ONSUBMIT="return confirmation( )">
<table>
<tr><td><input type=radio name='mainttask' checked value='clearsheet'></td><td>Clear the signup sheet</td></tr>发布于 2013-03-06 14:22:55
<script type="text/javascript">
function confirmation( )
{
if (maint.mainttask.value == "clearsheet")
{
var answer = confirm('Are you sure you want to clear all entries?');
if (answer){
return true;
}
else{
return false;
}
}
return true;
}
</script>
<form method='post' action='#' name='maint' ONSUBMIT="return confirmation( )">
<table>
<tr><td><input type=radio name='mainttask' checked value='clearsheet'></td><td>Clear the signup sheet</td></tr>
<input type="submit" name="submit" value="submit"/>
</form>尝试这个编辑过的代码,希望现在它能为您工作并显示确认框。
发布于 2013-03-06 15:42:50
我总是喜欢使用标准按钮,并从验证函数中调用submit()方法,而不是依赖提交按钮和onsubmit事件。其他人可能不同意,但在我看来,通过这种方式你可以更好地控制事情。尝试执行以下操作...
<html>
<head></head>
<body>
<script type="text/javascript">
function confirmation() {
if (maint.mainttask.value == "clearsheet") {
var answer = confirm('Are you sure you want to clear all entries?');
if (answer==true){
document.forms["maint"].submit();
}
}
}
</script>
<form method='post' action='path removed' name='maint'>
<table>
<tr><td><input type='radio' name='mainttask' checked value='clearsheet'></td><td>Clear the signup sheet<br />
<input type="button" onClick="confirmation()" value="Click me..."/></td></tr>
</table>
</form>
</body>
https://stackoverflow.com/questions/15240304
复制相似问题