javascript会自动调用

时间:2015-09-02 14:27:02

标签: javascript jsp

我有一个javascript函数,每次重新加载我的jsp时都会自动处理..虽然我应该按下按钮点击它...为什么会这样?

也许我的语法有错误,你能帮忙吗? 这是代码:

<form class="form-grp clearfix mse_pre_init" id="widgetu599" method="post" enctype="multipart/form-data" ><!-- none box -->
 <div class="clearfix grpelem" id="u604-4"><!-- content -->
  <p>Submitting Form...</p>
 </div>
 <div class="clearfix grpelem" id="u606-4"><!-- content -->
  <p>The server encountered an error.</p>
 </div>
 <div class="clearfix grpelem" id="u605-4"><!-- content -->
  <p>Form received.</p>
 </div>
 <input class="submit-btn NoWrap grpelem" id="u607-17" type="submit" value="" tabindex="3" onClick = "printform()"/><!-- state-based BG images -->
<script>


function printform(){

    admit();

}

function admit(){
       <%
        String assessment_id = "";
        String section = "";
        String s_id = (String)session.getAttribute("id");
        String payment1;

        PreparedStatement pst, pst1;
        Connection con;
        ResultSet rs;

    try{

        String sql2="select * from assessment_tbl order by assessment_id + 1";
        Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection("jdbc:mysql://localhost:3306/system", "root","");
    pst = con.prepareStatement(sql2);
    rs = pst.executeQuery();
        if(rs.last()){

            String ass = rs.getString("assessment_id");
            int a = Integer.parseInt(ass);
            int b = a + 1;
            String asss = Integer.toString(b);
            assessment_id = asss;

        }
        else{

            assessment_id = "1";

        }


    {%>
    var a = document.getElementById("payment");
    var b = a.options[a.selectedIndex].text;

    if(b === "Full Payment"){
        if(document.getElementById("id1").checked || document.getElementById("id2").checked || document.getElementById("id3").checked){

            alert('Select Insallment First!');

        }
        else{
        <%payment1="1";
        String sqlhaha= "Insert into assessment_tbl values ('"+assessment_id+"', '"+con_num+"', '"+section+"', '"+s_id+"', '"+payment1+"', '"+year1+"')";
        pst1=con.prepareStatement(sqlhaha);
        pst1.executeUpdate();

        %>



    }
    }
    else{

        if(document.getElementById("id1").checked){




        }
        else if(document.getElementById("id2").checked){



        }
        else if(document.getElementById("id3").checked){



        }
        else{
            alert('Select Semestral/Quarterly/Monthly');
        }

    }
    <%}





    }
    catch(Exception e){



    }

%> 


}
</script>
</form>

1 个答案:

答案 0 :(得分:0)

我相信你错了,在你的页面被发送到浏览器之前jsp会发生什么。请记住,在将页面发送到客户端之前,会执行每个Java代码<% java code %>。触发javascript onclick操作后,Java代码(服务器代码)将无法执行(客户端函数admit())。

在呈现页面时执行Java代码<% %>。因此,每次重新加载页面时,每个Java代码都会发生。

如果您需要使用JavaScript从客户端(浏览器)到服务器(没有客户端重新编码页面)进行通信,则需要使用异步技术AJAX(Async.JavaScript和Xml)。不要混淆标题,你可以使用例如json进行数据交换。