首页 > 解决方案 > jsp-无法在 Onclick 属性中调用 javascript 函数

问题描述

我在 body 标记中编写了一个简单的 javascript 函数:

 <script >
        function update(param){
            var ut = document.getElementById("usertype"+param);
            alert(ut);
        };


    </script>

我想在我的 onclick 按钮中调用这个函数,如下所示:

 <%
        try {


            String str = "<table border=2><tr><th>Username</th><th>New Username</th><th>User Type</th><th>Update</th></tr>";
              int rowid = 0;
            while (rs.next()) {
                String autype = rs.getString("ausertype");
                str += "<tr><td>" + rs.getString("ausername") + "</td> <td><input type=\"text\" name=\"NewUserName\" " +
                        "value=\"\"></td> <td>";

                if (autype.equalsIgnoreCase("Superuser")) {
                    str += "<input type=\"radio\" name=\"usertype"+rowid+"\"  value=\"Superuser\" checked> Superuser ";
                }else{
                    str += "<input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Superuser\" > Superuser ";
                }
                if(autype.equalsIgnoreCase("Basic")) {
                    str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Basic\"  checked > Basic ";
                }else {
                    str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"Basic\"   > Basic ";
                }
                if(autype.equalsIgnoreCase("View")){
                    str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"View\"  checked> View ";
                }else {
                    str += " <input type=\"radio\" name=\"usertype"+rowid+"\" value=\"View\"  > View ";
                }
                str += "</td><td><button type=\"button\" onclick="+update(rowid)+">Update User</button></td> </tr>";
            rowid ++;

            }

%>

如上所示,我无法解析updateonclick按钮中的方法,因为它包含在Java标签中<% %>。当我删除标签时,它可以解析javascript函数。我需要为我的代码附上 java 标记。

这个问题有什么解决方法吗?

标签: javascriptjsp

解决方案


尝试以下方式编写:

str += "</td><td><button type=\"button\" onclick='update('+rowid+')'>Update User</button></td> </tr>";

推荐阅读