2015-02-11 91 views
0

任何人都可以告诉我为什么下面的JavaScript函数没有调用时点击按钮或提交表单。Javascript函数不叫onclick()和onsubmit()

我不明白这是这个代码的错误。请帮助

<script type="text/javascript"> 
 

 
function confrm() 
 
{ 
 
\t alert("check1"); 
 
\t if(confirm("Are you going to submit this form?")==true) 
 
\t { 
 
\t \t return true; 
 
\t } 
 
\t else 
 
\t { 
 
\t \t return false; 
 
\t } 
 
} 
 

 
function checkfrm() 
 
{ 
 
\t var selcteddate=document.newrequest.sel.value; 
 
\t var sdate=new Date(selcteddate); 
 

 
alert("check 2"); 
 

 
\t if(document.newrequest.server.value=="select") 
 
\t { 
 
\t \t alert("Select server"); 
 
\t \t return false; 
 
\t } 
 

 
\t else 
 
\t { 
 
\t \t return true; 
 
\t } 
 
} 
 

 
</script> 
 

 

 

 

 
<form id="newrequest" name="newrequest" method=post action="cgi-bin/newrequest.cgi" onsubmit="return checkfrm();"> 
 

 
<td colspan=2> 
 
<input type="submit" id="submit" name="submit" value="Submit" onclick="return confrm();" /> 
 
</td> 
 
    
 
    
 

+0

你有没有真正尝试自己的代码片段提交指南?这对我来说可以。 – JJJ 2015-02-11 08:45:16

+1

如果它在“真实”代码中不适用于您,请打开浏览器的错误控制台。很可能是您的代码中有可能停止脚本执行的其他地方出现语法错误。 – JJJ 2015-02-11 08:45:54

+0

代码段也适用于我 – donnywals 2015-02-11 08:46:17

回答

-1

你可以做没有回报。试试这个:

<script type="text/javascript"> 
    function confrm(){ 
     alert("check1"); 
     if(confirm("Are you going to submit this form?")==true) {    
      document.getElementById("newrequest").submit(); 
     }else{ 
      return false; 
     } 
    } 
    </script> 
<form id="newrequest" name="newrequest" method="post" action="cgi-bin/newrequest.cgi" onsubmit="checkfrm();"> 
<td colspan=2> 
<input value="Submit" type="input" onclick="confrm();" /> 
</td> 

你可以找到 http://www.w3schools.com/jsref/event_onclick.asp 用于onclick事件的引导和 http://www.w3schools.com/jsref/met_form_submit.asp

+0

请不要参考W3Schools,请使用相关规范作为权威建议和MDN示例。 – RobG 2015-02-11 09:11:49

+0

删除退货只会使无法阻止表单提交。 – JJJ 2015-02-11 10:10:06