javascript - 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 ++;
}
%>
如上所示,我无法解析update
onclick按钮中的方法,因为它包含在Java标签中<% %>
。当我删除标签时,它可以解析javascript函数。我需要为我的代码附上 java 标记。
这个问题有什么解决方法吗?
解决方案
尝试以下方式编写:
str += "</td><td><button type=\"button\" onclick='update('+rowid+')'>Update User</button></td> </tr>";
推荐阅读
- java - 如何更改spring boot h2控制台的端口
- php - 如何在 AJAX 调用中检查 JSON 对象中是否有数据
- java - 为什么子类可以使用其他包中类的受保护字段?
- angular - 如何在特定文件夹上运行 ng lint - 不是通过排除,而是通过包含某些文件夹?
- json - JIRA API POST jql 搜索
- sql - Oracle SQl Dev,显示 id、firstname、surname 和 users 文件数
- c++ - 使用字符数组的字符串输入问题c ++
- javascript - tampermonkey 中带有 GM_xmlhttpRequest 的简单脚本每秒运行一次 - 内存泄漏
- python - Neovim 在运行 input() 时抛出错误,在 Vim 中运行正常
- android - 使用依赖项构建 AAR