我看到很多像这样的问题,但我的不同。我有一个表格,表格包含3个不同的选择区域,用于不同的变量。我想得到它们,但我不知道该怎么做,而且我是Javascript和HTML的新手。
编辑:我删除了"发送数据库"部分。这是背景的一部分。
这是HTML:
public String getDataValueAsString(Cell cell){
String value = null;
CellType type = cell.getCellTypeEnum();
DataFormatter dataFormat = new DataFormatter();
CreationHelper ch = null;
switch(type){
case BLANK:
value = "";
break;
case BOOLEAN:
value = String.valueOf(cell.getBooleanCellValue());
break;
case ERROR:
value = dataFormat.formatCellValue(cell);
break;
case FORMULA:
FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
value = dataFormat.formatCellValue(cell, evaluator);
break;
case NUMERIC:
if(DateUtil.isCellDateFormatted(cell)){
System.out.println("Cell is date formatted : ");
ch = cell.getSheet().getWorkbook().getCreationHelper();
short formatIndex = ch.createDataFormat().getFormat(cell.getCellStyle().getDataFormatString());
System.out.println("format index : "+formatIndex);
String format = cell.getCellStyle().getDataFormatString();
System.out.println("format : "+format);
}else{
//value = String.valueOf(cell.getNumericCellValue());
value = dataFormat.formatCellValue(cell);
}
break;
case STRING:
value = cell.getStringCellValue();
break;
default:
value = dataFormat.formatCellValue(cell);
}
return value;
}
假设用户选择得分7,将语言设置为波斯尼亚语,年份为1980年(我填写年份框中的函数并不担心它不是空列表)我希望得到这些选定的值(得分:7,郎:波斯尼亚,年份:1980)顺便说一句,所有这些都是形式。那么我怎样才能获得这些选定的值?
答案 0 :(得分:2)
HTML中存在以下错误:
<option>
代码属性。所以在这段代码中:
<select name="scores">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
</select>
您可以将“得分”值视为:
var select_scores = document.getElementById("form").scores;
select_scores.options[select_scores.selectedIndex].value;
其他形式的<select>
元素相同。
答案 1 :(得分:1)
返回一个对象:
document.getElementById("submit").addEventListener("click", getData);
function getData() {
var score = document.getElementById('scores').value;
var year = document.getElementById('year').value;
var language = document.getElementById('language').value;
var data = {'score':score, 'year':year, 'lang':language};
console.log(data);
}
<form>
<p> Scores: </p>
<select id ="scores" name ="scores:">
<option value= " 9 " name= " 9 "> 9 </option>
<option value= " 8 " name= " 8 "> 8 </option>
<option value= " 7 " name= " 7 "> 7 </option>
<option value= " 6 " name= " 6 "> 6 </option>
<option value= " 5 " name= " 5 "> 5 </option>
<option value= " 4 " name= " 4 "> 4 </option>
<option value= " 3 " name= " 3 "> 3 </option>
<option value= " 2 " name= " 2 "> 2 </option>
<option value= " 1 " name= " 1 "> 1 </option>
</select>
<p> Years: </p>
<select id ="year">
</select>
<p> Languages: </p>
<select id= "language" name="language">
<option value ="Aborginial", name = "Aborginial"> Aborginial </option>
<option value="Arabic"> Arabic </option>
<option value="Aramaic">Aramaic</option>
<option value="Bosnian">Bosnian</option>
<option value="Cantonese">Cantonese</option>
<option value="Chinese">Chinese</option>
<option value="Czech">Czech</option>
</select>
<button id="submit" type="button">Submit data</button>
</form>