从JSON填充的选项选择中传递元素到控制器。

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

Pass element from option select populated from JSON to Controller

问题

以下是您提供的内容的翻译部分:

这是我的第一篇帖子,几个月来我能够找到正确的答案,但这一次卡住了。
我设法在JavaScript中创建了函数来创建依赖的级联下拉列表,当我选择汽车品牌时,下一个列表中会显示型号,再下一个列表中会显示系列。
但是无法将选择的位置发送到Java控制器。我对批评持开放态度 从JSON填充的选项选择中传递元素到控制器。 我会非常感谢任何帮助。

因此,视图如下所示:

<form action="make" method="POST">
    <div class="item">
        <h3 class="item-title">选择汽车</h3>
        <span class="year"></span>
        <select id="name" name="name" class="wrapper" onchange="populateSelectModel(), populateSelectSeries()">
            <option value="">--选择--</option>
        </select>
        <select id="model" name="model" onchange="populateSelectSeries()">
            <option value="">--选择--</option>
        </select>
        <select id="seria" name="seria">
            <option value="">--选择--</option>
        </select>
        <input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
        <button type="submit" class="btn-primary">提交</button>
    </div>
</form>

JavaScript部分:

var selectBrand = document.getElementById("name");
var selectModel = document.getElementById("model");
var selectSeries = document.getElementById("seria");
var ourData;
var ourRequest = new XMLHttpRequest();
ourRequest.open('GET', 'https://raw.githubusercontent.com/ahmetozalp/auto-cars-makes-models-types/master/cars.json');
ourRequest.onload = function(){
    ourData = JSON.parse(ourRequest.responseText);
    var selectedBrand = populateSelectBrand(ourData);
    populateSelectModel();
    populateSelectSeries();
};
ourRequest.send();

function populateSelectBrand(lista) {
    for (var i = 0; i < lista.length; i++) {
        selectBrand.innerHTML = selectBrand.innerHTML +
            '<option value="">' + lista[i].brand + '</option>';
    }
}

function populateSelectModel() {
    selectModel.innerHTML = '<option value="">选择型号';
    var jakIndex = selectBrand.selectedIndex - 1;
    var howlong = ourData[jakIndex].models.length;
    for (var i = 0; i < howlong; i++) {
        selectModel.innerHTML = selectModel.innerHTML +
            '<option value="">' + ourData[jakIndex].models[i].title + '</option>';
    }
}

function populateSelectSeries() {
    selectSeries.innerHTML = '<option value="">选择系列';
    var jakiIndex = selectBrand.selectedIndex - 1;
    var jakiIndexModelu = selectModel.selectedIndex - 1;
    var howlong1 = ourData[jakiIndex].models[jakiIndexModelu].types;
    for (var i = 0; i < howlong1.length; i++) {
        selectSeries.innerHTML = selectSeries.innerHTML +
            '<option value="">' + ourData[jakiIndex].models[jakiIndexModelu].types[i] + '</option>';
    }
}

最后,当我可以通过控制器从"name"、"model"、"series"中获取选择的值时,我得到的是空字符串(而不是null)。

Java控制器:

@Controller
@RequestMapping("make")
public class MakeController {
    @PostMapping
    public String downloadMake(String name, String model, String seria) throws IOException {
        return ("download");
    }
}

从IntelliJ的屏幕截图,调试时的情况

英文:

This my very first post, for several months I was able to find correct answer but this time I stuck.
I manage to create in JavaScript functions to create dependent - cascade drop-down-lists, when I choose Car Brand, then Models are served in next list and series in another accordingly.
But can not send the chosen positions to Java Controller. Im open to criticism :-) Ill be very grateful for any help.

So the view looks as follows:

<!-- language: lang-html -->

	&lt;form  action=make method=&quot;POST&quot;&gt;
&lt;div class=&quot;item&quot;&gt;
&lt;h3 class=&quot;item-title&quot;&gt;Choose car&lt;/h3&gt;
&lt;span class=&quot;year&quot;&gt;&lt;/span&gt;
&lt;select id=&quot;name&quot; name=&quot;name&quot; class=&quot;wrapper&quot;  onchange=&quot;populateSelectModel(), populateSelectSeries()&quot;&gt;
&lt;option   value=&quot;&quot;&gt;--Select--&lt;/option&gt;
&lt;/select&gt;
&lt;select id=&quot;model&quot; name=&quot;model&quot; onchange=&quot;populateSelectSeries()&quot; &gt;
&lt;option value=&quot;&quot;&gt;--Select--&lt;/option&gt;
&lt;/select&gt;
&lt;select id=&quot;seria&quot; name=&quot;seria&quot;&gt;
&lt;option value=&quot;&quot;&gt;--Select--&lt;/option&gt;
&lt;/select&gt;
&lt;input type=&quot;hidden&quot; th:name=&quot;${_csrf.parameterName}&quot; th:value=&quot;${_csrf.token}&quot;/&gt;
&lt;button type=&quot;submit&quot; class=&quot;btn-primary&quot;&gt;Submit&lt;/button&gt;
&lt;/div&gt;
&lt;/form&gt;

<!-- end snippet -->

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var selectBrand = document.getElementById(&quot;name&quot;);
var selectModel = document.getElementById(&quot;model&quot;);
var selectSeries = document.getElementById(&quot;seria&quot;);
var ourData;
var ourRequest = new XMLHttpRequest();
ourRequest.open(&#39;GET&#39;, &#39;https://raw.githubusercontent.com/ahmetozalp/auto-cars-makes-models-types/master/cars.json&#39;);
ourRequest.onload = function(){
ourData = JSON.parse(ourRequest.responseText);
var selectedBrand = populateSelectBrand(ourData);
populateSelectModel();
populateSelectSeries();
};
ourRequest.send();
function populateSelectBrand(lista) {
for (var i = 0; i &lt; lista.length; i++) {
selectBrand.innerHTML = selectBrand.innerHTML +
&#39;&lt;option value=&quot;&quot;&gt;&#39; + lista[i].brand + &#39;&lt;/option&gt;&#39;;
}
}
function populateSelectModel() {
selectModel.innerHTML = &#39;&lt;option value=&quot;&quot;&gt;Select model&#39;;
var jakIndex = selectBrand.selectedIndex - 1;  
var howlong = ourData[jakIndex].models.length;
for (var i = 0; i &lt; howlong; i++) {
selectModel.innerHTML = selectModel.innerHTML +
&#39;&lt;option value=&quot;&quot;&gt;&#39; + ourData[jakIndex].models[i].title + &#39;&lt;/option&gt;&#39;;       
}
}
function populateSelectSeries() {
selectSeries.innerHTML = &#39;&lt;option value=&quot;&quot;&gt;Select series&#39;;
var jakiIndex = selectBrand.selectedIndex -1 ;  
var jakiIndexModelu = selectModel.selectedIndex-1;
var howlong1 = ourData[jakiIndex].models[jakiIndexModelu].types;
for (var i = 0; i &lt; howlong1.length; i++) { 
selectSeries.innerHTML = selectSeries.innerHTML +
&#39;&lt;option value=&quot;&quot;&gt;&#39; + ourData[jakiIndex].models[jakiIndexModelu].types[i] + &#39;&lt;/option&gt;&#39;;       
}
}

<!-- end snippet -->

==================================================================
Finally when I can get via Controller the selected values from "name","model","series" I`m getting empty Strings (not null)
Java Controller

@Controller

@RequestMapping("make")
public class MAkeController {

@PostMapping
public String downloadMake(String name, String model, String seria) throws IOException {
return (&quot;download&quot;);
}

}

screen shot from IntelliJ, while debugging

答案1

得分: 0

在填充select下拉选项时,在所有三个函数中,您将option标签的设置为空字符串 value=&quot;&quot;,如下所示:

&lt;option value=&quot;&quot;&gt;

您还需要在option标签的value属性中设置相应的item

例如,在populateSelectBrand函数中类似于以下内容:

selectBrand.innerHTML = selectBrand.innerHTML + &#39;&lt;option value=&quot;&#39; + lista[i].brand + &#39;&quot;&gt;&#39; + lista[i].brand + &#39;&lt;/option&gt;&#39;;

在其他函数中也要执行相同的操作。

当您执行form提交时,将向服务器发送所选选项的值,而不是在UI屏幕上显示的label

英文:

While populating the select drop-down options, you are setting option tag value as empty string value=&quot;&quot; in all the three functions as below

&lt;option value=&quot;&quot;&gt;

You need to set the respective item in value attribute of option tag too.

For example, similar to below in populateSelectBrand function:

selectBrand.innerHTML = selectBrand.innerHTML + &#39;&lt;option value=&quot;&#39; + lista[i].brand + &#39;&quot;&gt;&#39; + lista[i].brand + &#39;&lt;/option&gt;&#39;;

Do the same in other functions too.

When you do form submit, the selected option value will be sent to the server not the label displaying in thr UI screen.

huangapple
  • 本文由 发表于 2020年7月25日 00:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63077782.html
匿名

发表评论

匿名网友

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

确定