如何在JavaScript中使用树状图(treemap)

huangapple go评论107阅读模式
英文:

How to use treemap in java script

问题

以下是您要翻译的内容:

"我从Java Servlet发送了一个树状图到JSP。我能够在JSP页面内使用树状图的键作为下拉列表。我的地图包含国家名称作为键,首都作为值,当用户选择国家名称时,我只需要在文本框中显示国家首都。我需要在表单提交后在Java页面中获取所选的键值(国家名称),我知道如何做这个。当我从JavaScript中读取地图时,它被识别为字符串/[object Object]。我如何在这里使用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>

<details>
<summary>英文:</summary>

I am sending one treemap from java servlet to jsp. I am able to use the treemap keys as drop down list inside jsp page . My map contains country name as key and capital as value when user selected country name i just need to display country capital in the text box.I need selected key value(country name) in the java page after form submission(I know hot to do this).when i am reading map from java script it is taking as string/[object Object]. how can i use map.get(key) here. Thanks in advance 


 

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

   



</details>


# 答案1
**得分**: 0

Finally, I found the answer myself. I have converted treemap to json object in the servlet class and pushed to jsp.

Servlet Java:

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

JavaScript function:

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

Import gson-2.8.0.jar to the class path. It works fine for me, not sure if it is the right way.

英文:

Finally, I found the answer myself.I have converted treemap to json object in the servlet class and pushed to jsp

Servelt java

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

javascript function

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

import gson-2.8.0.jar to the class path. It works fine for me, not sure it is a right way.

huangapple
  • 本文由 发表于 2020年7月31日 16:50:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63188675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定