2013-02-20 194 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)