gpt4 book ai didi

javascript - JSP 中的 javascript 代码有问题吗?

转载 作者:行者123 更新时间:2023-11-28 05:19:54 25 4
gpt4 key购买 nike

我试图通过单击 jsp 表单中的按钮来获取按钮值。但由于某种原因,当我单击按钮时,它总是显示“1”。那么,我可能做错了什么?我仍在学习 Javascript,并且我实现了一个小脚本来在单击按钮时显示按钮值。我觉得我的 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='Pramodk@CS.com' />
</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>

最佳答案

注意事项:1)ID仅适用于html标记中的单个元素(您的按钮都具有相同的id“getid”,不应该这样做),如果您想分配单个名称,请使用类/将字符串定位到多个元素

2)您可以在 html 标记中使用 this 来定位函数分配给的元素,检查我更新的代码以完全理解我在说什么

<!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='Pramodk@CS.com' />
</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>

关于javascript - JSP 中的 javascript 代码有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647993/

25 4 0
文章推荐: c++ - 匹配 Eigen 和 Ceres-Solver 版本的最简单方法是什么?
文章推荐: javascript - 在javascript中的函数内传递函数的参数
文章推荐: HTML 表格截断文本但尽可能适合
文章推荐: javascript - 根据两个