2016-11-17 24 views
1

我試圖通過單擊jsp窗體中的按鈕來獲取按鈕值。但由於某種原因,當我點擊按鈕時,它總是顯示「1」。那麼,我會做什麼錯?我仍然在學習Javascript,並且實現了一個小腳本來顯示按鈕的值,當我點擊它時。我覺得我在我的Javascript代碼中犯了一個錯誤。請幫助我。JSP中JavaScript代碼的問題?

JSP頁面: -

<table 
      <tr> 
       <td colspan=4 align="center" 
        style="background-color:teal"> 
        <b>User Record</b></td> 
      </tr> 
      <tr style="background-color:lightgrey;"> 
       <td><b>Record Number: </b></td> 
       <td><b>Card Number: </b></td> 
       <td><b>MiddleName:</b></td> 
       <td><b>BankAccountID:</b></td> 
       <td><b>CurrencyID:</b></td> 
       <td><b>DayTransactionLimit:</b></td> 
       <td><b>Select:</b></td> 


</tr> 
    <% 
     if(request.getParameter("mobilenumber")!=null) 
     {   
FileReader fr = new FileReader(new File(("C:\\Users\\Surajitdas\\Documents\\NetBeansProjects\\Demo\\records.txt"))); 
    BufferedReader br = new BufferedReader(fr); 
    String line = null; 
    int count =0; 
// out.println("<table>"); 

    while((line = br.readLine()) != null){ 
    //out.print(line + "<br/>"); 
     out.println("<tr>"); 
      count ++; 

    String[] data = line.split("\t"); 
    for (String val : data) { 

     out.println("<td>" + val + "</td>"); 


    } 

    out.println("<td><input type =\"button\" id=\"getid\" onclick=\"getbuttonid()\" value=\"" + count + "\"></td>"); 
    } 
    out.println(""); 
    br.close(); 



     } 

%> 

    </table>   






</table 

    <script type="text/javascript"> 
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); 
function getbuttonid(){ 
    var id = document.getElementById('getid').value; 
    alert(id); 
} 

</script> 

JSP頁面

<!DOCTYPE html> 

<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>HomePage</title> 
<script src="JavaScript/SpryMenuBar.js" type="text/javascript"></script> 
<link href="styles/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" /> 
</head> 


<ul id="MenuBar1" class="MenuBarVertical"> 
    <li><a href="#">Update Limit</a></li> 
    </li> 
    <li><a href="#">Delink Account</a></li> 
    <li><a href="#">Edit CardNumber</a> 

    </li> 
    <li> 
    <a href="#">Linked Accounts</a> 
    </li> 
    <li><a href="#">SMS Alerts</a> 
    <li><a href="#">Join Account</a> 


</ul> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 


    <hr /> 
<hr /> 
<form> 
    <p> 
      Client Details 
    <hr /> 
    <hr /> 
    </p> 
    <p>Mobile Number:- 
    <label> 
     <input type="text" name="mobilenumber" id="mobilenumber" /> 
    </label> 

    </p> 
<p>Country:- 
    <label> 
     <input type="text" name="country" id="country" /> 
    </label> 

    </p> 

<input type="submit" formaction="Search" formmethod="post" name="New" id="New" value="Search" /> 





    <p> Title:- 
    <input type="text" name="Title" id="Title" value='Mr' /> 
    </p> 
    <p>Middle Name:- 
    <label> 
     <input type="text" name="MiddleName" id="Middle Name" /> 
    </label> 
    </p> 
    <p>Address:- 
    <label> 
     <input type="text" name="address" id="address" /> 
    </label> 
    </p> 
    <p>City:- 
    <label> 
     <input type="text" name="City" id="City"value='AWENDO'/> 
    </label> 
    </p> 
    <p>Type of ID:- 
    <label> 
     <input type="text" name="Idtype" id="Idtype" value='Driving License' /> 
    </label> 
    </p> 
    <p>Elma ID:- 
    <label> 
     <input type="text" name="elmaid" id="elmaid" value='1916507095'/> 
    </label> 
    </p> 
    <p>First Name:- 
    <label> 
     <input type="text" name="firstname" id="firstname" value='Pramod1' /> 
    </label> 
    </p> 
    <p>Last Name:- 
    <label> 
     <input type="text" name="LastName" id="LastName" value='Sahoo'/> 
    </label> 
    </p> 
    <p>Registration Branch:- 
    <label> 
     <input type="text" name="registrationbranch" id="registrationbranch" /> 
    </label> 
    </p> 
    <p>Email address:- 
    <label> 
     <input type="text" name="emailid" id="emailid" value='[email protected]' /> 
    </label> 
    </p> 
    <p>ID No:- 
    <label> 
     <input type="text" name="idno" id="idno" /> 
    </label> 
    </p> 
    <hr /> 
<hr /> 
    <p> 
      Account Details 
    <hr /> 
    <hr /> 
    </p> 

    <p>Bank A/C id :- 
    <label> 
     <input type="text" name="accountid" id="accountid"value='001001000095' /> 
    </label> 
    </p> 
    <p>Transaction Limit:- 
    <label> 
     <input type="text" name="transactionlimit" id="transactionlimit" /> 
    </label> 
    </p> 
    <p>Currency:- 
    <input type="text" name="currency" id="currency"value='KES'/> 
    </p> 
    <p>Daily Limit:- 
    <label> 
     <input type="text" name="dailylimit" id="dailylimit" value='500000.0000'/> 
    </label> 
    </p> 
    <p> 
    <input type="submit"formaction="Connectioncheck" formmethod="post"name="Update" id="Update" value="Update" /> 
    </p> 
    <p>&nbsp;</p> 
    <hr /> 
    Account List 
    <hr /> 



    <table 
      <tr> 
       <td colspan=4 align="center" 
        style="background-color:teal"> 
        <b>User Record</b></td> 
      </tr> 
      <tr style="background-color:lightgrey;"> 
       <td><b>Record Number: </b></td> 
       <td><b>Card Number: </b></td> 
       <td><b>MiddleName:</b></td> 
       <td><b>BankAccountID:</b></td> 
       <td><b>CurrencyID:</b></td> 
       <td><b>DayTransactionLimit:</b></td> 
       <td><b>Select:</b></td> 


</tr> 
    <tr> 
<td>1</td> 
<td>kenya</td> 
<td>K</td> 
<td>432342423</td> 
<td></td> 
<td>100000.0000</td> 
<td><input type ="button" id="getid" onclick="getbuttonid()" value="1"></td> 
<tr> 
<td>2</td> 
<td>kenya</td> 
<td>Kumar11</td> 
<td>78788787878</td> 
<td>OOOPP</td> 
<td>100000.0000</td> 
<td><input type ="button" id="getid" onclick="getbuttonid()" value="2"></td> 
<tr> 
<td>3</td> 
<td>kenya</td> 
<td>Kb1</td> 
<td>001001000095</td> 
<td>KES</td> 
<td>500000.0000</td> 
<td><input type ="button" id="getid" onclick="getbuttonid()" value="3"></td> 



    </table>   






</table 


    <hr /> 
    Behind the Scene 
    <hr /> 







    <p>Created By:- 
    <label> 
     <input type="text" name="createdby" id="createdby" /> 
    </label> 
    </p> 
    <p>Created On:- 
    <label> 
     <input type="text" name="createdon" id="createdon" /> 
    </label> 
    </p> 
    <p>Modified By:- 
    <label> 
     <input type="text" name="modifiedby" id="modifiedby" /> 
    </label> 
    </p> 
    <p>Modified On:- 
    <label> 
     <input type="text" name="modifiedon" id="modifiedon" /> 
    </label> 
    </p> 
    <p>Supervised By:- 
    <label> 
     <input type="text" name="supervisedby" id="supervisedby" /> 
    </label> 
    </p> 
    <p>Supervised On:- 
    <label> 
     <input type="text" name="supervisedon" id="supervisedon" /> 
    </label> 
    </p> 
    <p> 
     <input type="submit" name="Add" id="Add" value="Add" formmethod="post" formaction="AddController" /> 
    <input type="submit" name="Edit" id="Edit" value="Edit" formmethod="post" formaction="EditController" /> 
    <input type="submit" name="Save" id="Save" value="Save" /> 
    <input type="submit" name="Cancel" id="Cancel" value="Cancel" /> 
    <input type="submit" name="Recreate Key" id="Recreate Key" value="Recreate Key" /> 

    <input type="submit" name="Reset Password" id="Reset Password" value="Reset Password" /> 




    </p> 

</form> 

<script type="text/javascript"> 
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"}); 
function getbuttonid(){ 
    var id = document.getElementById('getid').value; 
    alert(id); 
} 

</script> 
+0

會再次運行你的應用程序,打開JSP你正在討論並複製它的全部內容?(通過你的瀏覽器的「查看頁面源代碼」)謝謝!你的javascript代碼是正確的,它可能是變量計數的值實際上是「1」 –

+0

@OmasuPlus: - 我已經添加了JSP頁面的整個代碼。謝謝 – Farheen

回答

0

說明你的完整代碼:1)ID僅用於在HTML標記的單個元素(你的按鈕都已經相同的ID「getid」,不應該這樣做),如果要將單個名稱/定位字符串分配給多個元素,請使用類別

2)您可以使用這個在HTML標記爲目標的功能被分配到元素,檢查我更新,以充分理解什麼IM代碼張口閉口約

<!DOCTYPE html> 
 

 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
 
<title>HomePage</title> 
 
<script src="JavaScript/SpryMenuBar.js" type="text/javascript"></script> 
 
<link href="styles/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" /> 
 
</head> 
 

 

 
<ul id="MenuBar1" class="MenuBarVertical"> 
 
    <li><a href="#">Update Limit</a></li> 
 
    </li> 
 
    <li><a href="#">Delink Account</a></li> 
 
    <li><a href="#">Edit CardNumber</a> 
 

 
    </li> 
 
    <li> 
 
    <a href="#">Linked Accounts</a> 
 
    </li> 
 
    <li><a href="#">SMS Alerts</a> 
 
    <li><a href="#">Join Account</a> 
 

 

 
</ul> 
 
<p>&nbsp;</p> 
 
<p>&nbsp;</p> 
 

 

 
    <hr /> 
 
<hr /> 
 
<form> 
 
    <p> 
 
      Client Details 
 
    <hr /> 
 
    <hr /> 
 
    </p> 
 
    <p>Mobile Number:- 
 
    <label> 
 
     <input type="text" name="mobilenumber" id="mobilenumber" /> 
 
    </label> 
 

 
    </p> 
 
<p>Country:- 
 
    <label> 
 
     <input type="text" name="country" id="country" /> 
 
    </label> 
 

 
    </p> 
 

 
<input type="submit" formaction="Search" formmethod="post" name="New" id="New" value="Search" /> 
 

 

 

 

 

 
    <p> Title:- 
 
    <input type="text" name="Title" id="Title" value='Mr' /> 
 
    </p> 
 
    <p>Middle Name:- 
 
    <label> 
 
     <input type="text" name="MiddleName" id="Middle Name" /> 
 
    </label> 
 
    </p> 
 
    <p>Address:- 
 
    <label> 
 
     <input type="text" name="address" id="address" /> 
 
    </label> 
 
    </p> 
 
    <p>City:- 
 
    <label> 
 
     <input type="text" name="City" id="City"value='AWENDO'/> 
 
    </label> 
 
    </p> 
 
    <p>Type of ID:- 
 
    <label> 
 
     <input type="text" name="Idtype" id="Idtype" value='Driving License' /> 
 
    </label> 
 
    </p> 
 
    <p>Elma ID:- 
 
    <label> 
 
     <input type="text" name="elmaid" id="elmaid" value='1916507095'/> 
 
    </label> 
 
    </p> 
 
    <p>First Name:- 
 
    <label> 
 
     <input type="text" name="firstname" id="firstname" value='Pramod1' /> 
 
    </label> 
 
    </p> 
 
    <p>Last Name:- 
 
    <label> 
 
     <input type="text" name="LastName" id="LastName" value='Sahoo'/> 
 
    </label> 
 
    </p> 
 
    <p>Registration Branch:- 
 
    <label> 
 
     <input type="text" name="registrationbranch" id="registrationbranch" /> 
 
    </label> 
 
    </p> 
 
    <p>Email address:- 
 
    <label> 
 
     <input type="text" name="emailid" id="emailid" value='[email protected]' /> 
 
    </label> 
 
    </p> 
 
    <p>ID No:- 
 
    <label> 
 
     <input type="text" name="idno" id="idno" /> 
 
    </label> 
 
    </p> 
 
    <hr /> 
 
<hr /> 
 
    <p> 
 
      Account Details 
 
    <hr /> 
 
    <hr /> 
 
    </p> 
 

 
    <p>Bank A/C id :- 
 
    <label> 
 
     <input type="text" name="accountid" id="accountid"value='001001000095' /> 
 
    </label> 
 
    </p> 
 
    <p>Transaction Limit:- 
 
    <label> 
 
     <input type="text" name="transactionlimit" id="transactionlimit" /> 
 
    </label> 
 
    </p> 
 
    <p>Currency:- 
 
    <input type="text" name="currency" id="currency"value='KES'/> 
 
    </p> 
 
    <p>Daily Limit:- 
 
    <label> 
 
     <input type="text" name="dailylimit" id="dailylimit" value='500000.0000'/> 
 
    </label> 
 
    </p> 
 
    <p> 
 
    <input type="submit"formaction="Connectioncheck" formmethod="post"name="Update" id="Update" value="Update" /> 
 
    </p> 
 
    <p>&nbsp;</p> 
 
    <hr /> 
 
    Account List 
 
    <hr /> 
 

 

 

 
    <table 
 
      <tr> 
 
       <td colspan=4 align="center" 
 
        style="background-color:teal"> 
 
        <b>User Record</b></td> 
 
      </tr> 
 
      <tr style="background-color:lightgrey;"> 
 
       <td><b>Record Number: </b></td> 
 
       <td><b>Card Number: </b></td> 
 
       <td><b>MiddleName:</b></td> 
 
       <td><b>BankAccountID:</b></td> 
 
       <td><b>CurrencyID:</b></td> 
 
       <td><b>DayTransactionLimit:</b></td> 
 
       <td><b>Select:</b></td> 
 

 

 
</tr> 
 
    <tr> 
 
<td>1</td> 
 
<td>kenya</td> 
 
<td>K</td> 
 
<td>432342423</td> 
 
<td></td> 
 
<td>100000.0000</td> 
 
<td><input type ="button" id="getid" onclick="getbuttonid(this)" value="1"></td> 
 
<tr> 
 
<td>2</td> 
 
<td>kenya</td> 
 
<td>Kumar11</td> 
 
<td>78788787878</td> 
 
<td>OOOPP</td> 
 
<td>100000.0000</td> 
 
<td><input type ="button" id="getid" onclick="getbuttonid(this)" value="2"></td> 
 
<tr> 
 
<td>3</td> 
 
<td>kenya</td> 
 
<td>Kb1</td> 
 
<td>001001000095</td> 
 
<td>KES</td> 
 
<td>500000.0000</td> 
 
<td><input type ="button" id="getid" onclick="getbuttonid(this)" value="3"></td> 
 

 

 

 
    </table>   
 

 

 

 

 

 

 
</table 
 

 

 
    <hr /> 
 
    Behind the Scene 
 
    <hr /> 
 

 

 

 

 

 

 

 
    <p>Created By:- 
 
    <label> 
 
     <input type="text" name="createdby" id="createdby" /> 
 
    </label> 
 
    </p> 
 
    <p>Created On:- 
 
    <label> 
 
     <input type="text" name="createdon" id="createdon" /> 
 
    </label> 
 
    </p> 
 
    <p>Modified By:- 
 
    <label> 
 
     <input type="text" name="modifiedby" id="modifiedby" /> 
 
    </label> 
 
    </p> 
 
    <p>Modified On:- 
 
    <label> 
 
     <input type="text" name="modifiedon" id="modifiedon" /> 
 
    </label> 
 
    </p> 
 
    <p>Supervised By:- 
 
    <label> 
 
     <input type="text" name="supervisedby" id="supervisedby" /> 
 
    </label> 
 
    </p> 
 
    <p>Supervised On:- 
 
    <label> 
 
     <input type="text" name="supervisedon" id="supervisedon" /> 
 
    </label> 
 
    </p> 
 
    <p> 
 
     <input type="submit" name="Add" id="Add" value="Add" formmethod="post" formaction="AddController" /> 
 
    <input type="submit" name="Edit" id="Edit" value="Edit" formmethod="post" formaction="EditController" /> 
 
    <input type="submit" name="Save" id="Save" value="Save" /> 
 
    <input type="submit" name="Cancel" id="Cancel" value="Cancel" /> 
 
    <input type="submit" name="Recreate Key" id="Recreate Key" value="Recreate Key" /> 
 

 
    <input type="submit" name="Reset Password" id="Reset Password" value="Reset Password" /> 
 

 

 

 

 
    </p> 
 

 
</form> 
 

 
<script type="text/javascript"> 
 

 
function getbuttonid(el){ 
 
    alert(el.value); 
 
} 
 

 
</script>

+0

非常感謝您的幫助。我有一個問題,這只是爲了我的好奇心。如果我想通過點擊按鈕將value = el.value傳遞給一個jsp標籤內的整數變量,我應該怎麼做呢? – Farheen

+0

var i = parseInt(el.value); –

+0

是否可以將var i發送到JSP標記內的整數變量? Java在服務器端執行,而JavaScript在客戶端執行。所以,這看起來有點困難。 – Farheen