如何通过单击使用循环动态创建的按钮来获取按钮的值

时间:2019-04-18 16:54:27

标签: javascript jsp servlets

i created a html button in jsp using while loop i want to get the value of the specific button that was clicked but it shown result as undefined.

我不知道该怎么办,我也不可能是Java Script的新手。     请提前帮助我。

  

Blockquote    这是我尝试过的方法,但无法给未定义的结果

    <td><button value="<%= path%>" onclick="myFunction()"><%= path %></button></td>

<script>
    function myFunction() {

        alert(this.value);

      <% session.setAttribute("gurufile",path );%>  
    }

</script>
  

Blockquote   这是我的整个代码,我从数据库中获取按钮的值

<%@page import="java.util.ArrayList"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.ResultSet"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <% 
            out.println(request.getParameter("roll_no"));

        %>   
<h3 align="center" style="color:BROWN;"> Dr.A.P.J. Abdul Kalam Technical University, Uttar Pradesh, Lucknow</h3>
<h4 align="center" >( Formerly Uttar Pradesh Technical University )</h4>
<h4  align="center" style="color:blue;">AKTU-OneView</h4>
</br>
<hr  align="center" style="color:lightgrey;">
<h3>Student Result</h3>
<%!                
    //golbally declear
                String class_name=null;
            String branch=null;
                String rollno =null;
                String path =null;
                ArrayList a = new ArrayList();
                int count =0;
                %>

    <% 
    //String roll_no = "1634010009";

       String roll_no = request.getParameter("roll_no");                               

      String url="jdbc:mysql://localhost/student_result";
        String username="root";
        String password="bilalminto";
        String query="select * from new_student where roll_no='"+roll_no+"'";
        System.out.println("rollno "+roll_no);

try{
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con=DriverManager.getConnection(url, username, password);
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query);
            String student_name=null;
            //String class_name=null;
            //String branch=null;
                //String rollno =null;
                String father_name = null;
                String mother_name = null;

            if(rs.next()){
                student_name=rs.getString("student_name");
                class_name=rs.getString("class");
                branch=rs.getString("branch");
                        rollno=rs.getString("roll_no");
                        father_name=rs.getString("fathers_name");
                        mother_name=rs.getString("mothers_name");
            }
            //Statement stm=con.createStatement();
            //ResultSet rst=stm.executeQuery(query);

    %>

<hr align="center" style="color:lightgrey;"></br>
<table width=100% border=1 cellspacing="0">
<tr align="left">
<td><b>Institute Code & Name :</b></td>
<td colspan="5">  (340) VIVEKANANDA COLLEGE OF TECHNOLOGY & MANAGEMENT, ALIGARH</td>
<td  rowspan="5">print view</td>
</tr>

<tr>
<td><b>Course Code & Name:</b></td>
<td colspan="2"><%=class_name%></td>
<td ><b>Branch Code & Name</b></td>
<td colspan="2"><%=branch%></td>
<%
System.out.println("branch "+branch);
%>
</tr>

<tr>
<td><b>Roll No:</b></td>
<td colspan="2"><%=roll_no %></td>
<td><b>Enrollment No</b></td>
<td colspan="2">163401047822</td>
</tr>

<tr>
<td><b>Name:</b></td>
<td colspan="2"><%=student_name%></td>
<td><b>Mother Name</b></td>
<td> <%=mother_name%></td>
</tr>

<tr>
<td><b>Father's Name:</b></td>
<td colspan="2"><%=father_name%></td>
<td><b>Gender</b></td>
<td colspan="2">M</td>
</tr>
</table></br>
<%
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                        session.setAttribute("roll_no", roll_no);
                    %>
<hr align="center" style="color:lightgrey;">
<h3>One View Result</h3>
<hr align="center" style="color:lightgrey;">

<%
    //String roll_no=session.getAttribute("roll_no").toString();


       String query1 = "select distinct semester from subject where class_name='"+class_name+"' && branch='"+branch+"'";


try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection con=DriverManager.getConnection(url, username, password);
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(query1);
                Statement st1 =con.createStatement();
                ResultSet rs1 =null;
                Statement st4 =con.createStatement();
                ResultSet rs4 =null;
                Statement st5 =con.createStatement();
                ResultSet rs5 =null;
            String semester =null;
            String subject_code=null;
                String subject_name=null;
                String max_marks=null;
                String marks_obtained = null;
                //String path =null;
                while(rs.next()){

                semester=rs.getString("semester");

                        %>

    <lable><b>Samester:</b></lable><label><%=semester%></label><lable><b>Total Subjects:</b></lable><label></label></br>
    <lable><b>Total Marks:</b></lable><label>900</label><lable><b>Marks Obt:</b></lable><label>500</label></br>
    </br>

    <table width=100% border=1 cellspacing="0">
    <tr>
        <td><b>Code</b></td>
        <td><b>Name</b></td>
        <td><b>Max Marks</b></td>
        <td><b>Marks Obtain</b></td>
        <td><input  type="text" name="" value="view copy"></td>
    </tr>

                        <%
                        String query2 ="select * from subject where semester='"+semester+"'";

                      rs1=st1.executeQuery(query2);


                        while(rs1.next()){
                subject_code=rs1.getString("subject_code");
                subject_name=rs1.getString("subject_name");
                max_marks=rs1.getString("max_marks");



                        String query3 ="select * from add_marks where roll_no='"+roll_no+"' and subject_code='"+subject_code+"'";
                        System.out.println(subject_code);

                        rs4 = st4.executeQuery(query3);


                        if(rs4.next() && rs4.getString("roll_no").equalsIgnoreCase(roll_no) && rs4.getString("subject_code").equalsIgnoreCase(subject_code)){
                       // if(rs4.next()){
                        marks_obtained=rs4.getString("marks_obtained");
                        path=rs4.getString("path");
                        }else{
                            marks_obtained = "Not Updated";
                            path = "Not Updated";
                        }
                        //session.setAttribute("gurufile",path );
                       a.add(count, path);
                       //out.println("pathof" +path);
                        %>

    <tr>
        <td><%=subject_code%></td>
        <td><%=subject_name%></td>
        <td><%=max_marks%></td>
        <td><%=marks_obtained%></td>

     <td><button value="<%= path%>" onclick="myFunction()"><%= path %></button></td>
      </tr> 


                        <%
                            count++;
                        }

            %>

    </table>
    </br>
    </br></br>
<%            

}

    %>


    <%
                    }
                    catch(Exception e){
                        e.printStackTrace();
                    }
                        session.setAttribute("roll_no", roll_no);
                    %>




</body>
</html> 


<script>
    function myFunction() {

        alert(this.value);

      <% session.setAttribute("gurufile",path );%>  
    }

</script>
  

Blockquote或使用任何其他方法来获取被单击按钮的值

1 个答案:

答案 0 :(得分:0)

您需要将this引用传递给该函数:

<button value="<%= path%>" onclick="myFunction(this)">

通过这种方式,您可以将单击按钮作为函数中的参数:

function myFunction(button) {
    alert(button.value);
}