Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用json从单选按钮输入显示动态mysql表_Java_Jquery_Html_Json_Servlets - Fatal编程技术网

Java 使用json从单选按钮输入显示动态mysql表

Java 使用json从单选按钮输入显示动态mysql表,java,jquery,html,json,servlets,Java,Jquery,Html,Json,Servlets,我有一个带有单选按钮的表单,当单击时,我将其传递给一个servlet,该servlet从mysql表中获取数据,并使用复选框将其显示在同一页面中 <input type="radio" name="id" value="1"> <input type="radio" name="id" value="2"> $(文档).ready(函数(){ $('input[name=id]')。更改(函数(){ var var_name=$(this.val(); conso

我有一个带有单选按钮的表单,当单击时,我将其传递给一个servlet,该servlet从mysql表中获取数据,并使用复选框将其显示在同一页面中

<input type="radio" name="id" value="1">
<input type="radio" name="id" value="2">


$(文档).ready(函数(){
$('input[name=id]')。更改(函数(){
var var_name=$(this.val();
console.log(变量名称);
$.getJSON(“ServletCode”,“id=”+var\u名称,函数(数据){
var myValues=data.toHtml;
$(“#guts”).html(myValues);
});
});
});        
我的servlet代码是:

JSONObject jsonObj = new JSONObject();
List<String> myList = new ArrayList<String>();
username = rs.getString("name");
id = rs.getString("userid");
myList.add(username); 
myList.add(userid);                
jsonObj.put("toHtml", myList.toArray());
response.setContentType("application/json");
response.getWriter().write(jsonObj.toString());
JSONObject JSONObject=new JSONObject();
List myList=new ArrayList();
用户名=rs.getString(“名称”);
id=rs.getString(“userid”);
myList.add(用户名);
添加(userid);
put(“toHtml”,myList.toArray());
setContentType(“应用程序/json”);
response.getWriter().write(jsonObj.toString());

我想使用这些值“id”和“name”以及复选框创建一个表。当选中复选框时,我需要再次显示从mysql获取的他的“标记”

如果我理解正确,您的JSON将

{"toHtml":["someNameValue", "someUserId"]}
但是,如果要有多个用户名id对,那么这些对可能需要自己的JSON对象。迭代它们会更容易

您要做的是使用javascript创建一个表

var table = "<table>";
table += "<thead><tr><th>username</th><th>id</th></tr></thead>";
table += "<tbody>";
// loop through the array elements and create rows
table += "</tr><td>" + /* get the username from the json */ + "</td>";
table += "<td>" + /* get the userid from the json */ + "</td>";
// add a javascript callback to a checkbox to display the grades in a similar way
// done loop
table += "</tbody>";
var表=”;
表+=“usernameid”;
表+=”;
//循环遍历数组元素并创建行
表+=''++/*从json*/+''获取用户名;
表+=“”+/*从json*/++“”获取用户ID;
//将javascript回调添加到复选框中,以类似的方式显示等级
//完成循环
表+=”;
然后,您可以将该表附加到其他html元素并使其可见

var table = "<table>";
table += "<thead><tr><th>username</th><th>id</th></tr></thead>";
table += "<tbody>";
// loop through the array elements and create rows
table += "</tr><td>" + /* get the username from the json */ + "</td>";
table += "<td>" + /* get the userid from the json */ + "</td>";
// add a javascript callback to a checkbox to display the grades in a similar way
// done loop
table += "</tbody>";