2016-11-17 64 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