英文:
Pass element from option select populated from JSON to Controller
问题
以下是您提供的内容的翻译部分:
这是我的第一篇帖子,几个月来我能够找到正确的答案,但这一次卡住了。
我设法在JavaScript中创建了函数来创建依赖的级联下拉列表,当我选择汽车品牌时,下一个列表中会显示型号,再下一个列表中会显示系列。
但是无法将选择的位置发送到Java控制器。我对批评持开放态度  我会非常感谢任何帮助。
 我会非常感谢任何帮助。
因此,视图如下所示:
<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");
    }
}
英文:
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 -->
	<form  action=make method="POST">
		<div class="item">
			<h3 class="item-title">Choose car</h3>
			<span class="year"></span>
			<select id="name" name="name" class="wrapper"  onchange="populateSelectModel(), populateSelectSeries()">
                <option   value="">--Select--</option>
            </select>
			<select id="model" name="model" onchange="populateSelectSeries()" >
                 <option value="">--Select--</option>
             </select>
             <select id="seria" name="seria">
                  <option value="">--Select--</option>
             </select>
			<input type="hidden" th:name="${_csrf.parameterName}" th:value="${_csrf.token}"/>
			<button type="submit" class="btn-primary">Submit</button>
		</div>
	</form>
<!-- end snippet -->
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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="">Select model';
      
            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="">Select series';
          
                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>';       
                }
              
            }
<!-- 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 ("download");
}
}
答案1
得分: 0
在填充select下拉选项时,在所有三个函数中,您将option标签的值设置为空字符串 value="",如下所示:
<option value="">
您还需要在option标签的value属性中设置相应的item。
例如,在populateSelectBrand函数中类似于以下内容:
selectBrand.innerHTML = selectBrand.innerHTML + '<option value="' + lista[i].brand + '">' + lista[i].brand + '</option>';
在其他函数中也要执行相同的操作。
当您执行form提交时,将向服务器发送所选选项的值,而不是在UI屏幕上显示的label。
英文:
While populating the select drop-down options, you are setting option tag value as empty string value="" in all the three functions as below
<option value="">
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 + '<option value="' + lista[i].brand + '">' + lista[i].brand + '</option>';
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.
专注分享java语言的经验与见解,让所有开发者获益!



评论