本文介绍了Multiselect列出JS中的选定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在提交表单之前在javascript中的Multiselect html下拉列表中选择值?请帮助
How to get the values selected in a Multiselect html dropdown list in javascript before submitting the form?Please help
推荐答案
<html>
<head>
<meta charset="UTF-8" />
<title>Multi Select</title>
<script language="JavaScript">
function getSelectedValues() {
var dropDown = document.getElementById('dd1'), countryArray = [], i;
for (i = 0; i < dropDown.options.length ; i += 1) {
if (dropDown.options[i].selected) {
//countryArray.push( dropDown.options[i].value); //If you need only values
countryArray.push({ Name: dropDown.options[i].text, Value: dropDown.options[i].value });
}
}
console.log(countryArray);
return false;
}
</script>
</head>
<body>
<form onsubmit="return getSelectedValues()">
<label>Multi Select dropdown </label>
<select id="dd1" multiple="true">
<option value="0">India</option>
<option value="1">Pakistan</option>
<option value="2">China</option>
<option value="3">Sri-Lanka</option>
<option value="4">Bangladesh</option>
</select>
<br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
这篇关于Multiselect列出JS中的选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!