2013-02-20 94 views
0

在下面提到的代码中,当我按下“拼写检查”按钮时,它不会调用javascript函数。寻找您的积极响应。 谢谢Not calling javascript function

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html 
    xmlns="http://www.w3.org/1999/xhtml"> 

    <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ taglib 
    uri="/WEB-INF/struts-bean.tld" prefix="bean"%> <%@ taglib 
    uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%> <%@ taglib 
    uri="/WEB-INF/struts-logic.tld" prefix="logic"%> 

<head>Welcome To struts Application. 

<title>Struts</title> 

<script language="javascript"> 

    function input(){  alert("Insie input function) 
      var abs = '<%=request.getContextPath()%>/index.do 
      System.out.println("Path"+abs); 
      alert(abs); 
      document.AccomodationForm.action=abs; 
      document.AccomodationForm.submit(); } 

</script> </head> 

<body> 

    <html:form method="post" action="/index"> <html:text 
    property="username"></html:text> <html:password 
    property="password"></html:password> <html:button value="Spellcheck" 
    property="button" onclick="javascript:input()"></html:button> 
    <html:link page="/index.do">Test the Action</html:link> </html:form> 


    </body> 


    </html> 

看看源代码,我看到了浏览器

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 







<head>Welcome To struts Application. 

<title>Struts</title> 

<script language="javascript"> 

    function input(){ 
     alert('Insie input function'); 
      var abs = '/MyWork/index.do 
      alert(abs); 

      document.AccomodationForm.action=abs; 
      document.AccomodationForm.submit(); 
    } 

</script> 
</head> 


<body> 

<form name="indexform" method="post" action="/MyWork/index.do;jsessionid=43C03A85FEBE58F375D2165C3E631111"> 
<input type="text" name="username" value=""> 
<input type="password" name="password" value=""> 
<input type="button" name="button" value="antriksh" onclick="input()"> 
<a href="/MyWork/index.do;jsessionid=43C03A85FEBE58F375D2165C3E631111">Test the Action</a> 
</form> 


</body> 


</html> 

问题是,它是将名称attribue在button.I不知道为什么?从你身边朋友的任何输入。

+0

如果这是你真正的代码中找到脚本错误,那么它在'input'功能的一些错字:年底分号在第一个'alert'调用中,关闭scriptlet的'''(事实上,scriptlet现在是一种不好的做法)。 – 2013-02-20 07:21:04

+0

对不起,我纠正了错字错误,但仍然没有调用输入函数。 – User1123123 2013-02-20 07:25:43

+0

@ANk:不是不行 – User1123123 2013-02-20 07:27:04

回答

0

onclick不带URL,所以javascript:部分是无关的。杀了它。

+0

我刚刚做了一个jsfiddle来测试这个,并发现它的工作原理。 – 2013-02-20 07:25:51

+0

我ahve删除了javascript:部分并纠正了一些错误的错误,但仍然没有调用输入函数。 – User1123123 2013-02-20 07:26:30

0

为什么你的JavaScript功能包括:的Java代码System.out.println("Path"+abs),并在你的JavaScript代码中有一些错误,看到以下内容:

function input(){  
     alert("Insie input function"); 
     var abs = '<%=request.getContextPath()%>/index.do'; 
     alert(abs); 
     document.AccomodationForm.action=abs; 
     document.AccomodationForm.submit(); 
} 
+0

这是比OP的代码= \ – 2013-02-20 07:23:48

+0

@Jason更糟糕:有一些错字错误,我纠正了。其他事情的代码是你张贴也错了,请警惕单列:) – User1123123 2013-02-20 07:32:47

+0

@ Antriksh现在有什么错误,你能看到在控制台或萤火虫错误? – Jason 2013-02-20 07:34:54

0

我猜固定错别字可以使你的代码工作:

function input() { 
    //close the String message for the alert function 
    alert("Insie input function)"; 
    //solve the scriptlet problem 
    var abs = '<%=request.getContextPath()%>' + '/index.do' 
    //THIS IS JAVA CODE, NOT JAVASCRIPT => System.out.println("Path"+abs); 
    alert(abs); 
    document.forms[0].action=abs; 
    document.forms[0].submit(); 

}

并遵循其他答案的建议,删除javascript:<html:button标签要素:

<html:button value="Spellcheck" 
    property="button" onclick="input()"></html:button> 
0

关闭单引号就能解决问题

变种ABS = '<%= request.getContextPath()%>' + '/index.do';

如果你使用Firefox,我们将使用错误控制台(按Ctrl + Shift + J)