【问题标题】:How to use treemap in java script如何在 JavaScript 中使用树形图
【发布时间】:2020-11-21 02:44:10
【问题描述】:

我正在从 java servlet 向 jsp 发送一个树形图。我可以使用树图键作为 jsp 页面内的下拉列表。当用户选择国家名称时,我的地图包含国家名称作为键,大写作为值我只需要在文本框中显示国家资本。我需要在提交表单后在 java 页面中选择键值(国家名称)(我知道要做这)。当我从 java 脚本中读取地图时,它被视为字符串/[对象对象]。我如何在这里使用 map.get(key)。提前致谢

function fillText(obj)
    {
               
               var key= obj.options[obj.selectedIndex].value;
           var map = new Map('<%=request.getAttribute("countrylist")%>');
           
           var contract = "${countrylist}";
           
           
    }
    <label for="Select The Switch">Select The Switch:</label>
           <select name="country" id="countrycode"  onchange="fillText(this)" required>
           <option value= "" >Please select the switch</option>
        <c:forEach items="${countrylist}" var="countrylist">
        <option value=${countrylist.key}>${countrylist.key}</option>
        </c:forEach>
       
            </select>`)  
    Country capital:<input type="text" id="pass" readonly >

【问题讨论】:

  • 你为什么混合jspjs?使用 ajax 将下拉列表的值发送到后端,然后获取您需要的值。

标签: javascript java jquery


【解决方案1】:

终于我自己找到了答案。我已经在servlet类中将treemap转换为json对象并推送到jsp

Servlet java

Gson gson = new Gson();
String json=gson.toJson(countrylist);
request.setAttribute("json", json);

javascript函数

function fillText(obj)
{
           
                  var key= obj.options[obj.selectedIndex].value;
                      var json=${json};
                  var value=json[key]
                  document.getElementById("pass").value=value;
                  
}

将 gson-2.8.0.jar 导入类路径。它对我来说很好,不确定它是否正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多