我的代码如下。我做了一个空数组,但我不知道如何连接它们。此外,我试图从我的组合框中获取值,但它给了我一个错误。文本框采用用户输入的值而不是组合框。
function sample() {
var jsObject = [];
jsObject {
"salutation": null,
"fname": null
};
var sal = document.getElementById('salutation').value;
var fname = document.getElementById('fname').value;
if (fname.value == "") {
return false;
}
for (var key in sal) {
sal = sal[key];
}
}
<!DOCTYPE html>
<html>
<head>
<title>Sample</title>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<form name="sam" onsubmit="return sample()" method="post">
<select name="salutation">
<option value="mr">Mr.</option>
<option value="mrs">Mrs.</option>
<option value="dr">Dr.</option>
</select><br> Name:
<br>
<input type="text" name="fname" id="fname" required placeholder="Enter name">
<input type="submit" onclick="sample(); return false;" value="Submit">
</form>
</body>
</html>
答案 0 :(得分:0)
如果您有一个如下所示的select元素:
<select id="ddlViewBy">
<option value="1">test1</option>
<option value="2" selected="selected">test2</option>
<option value="3">test3</option>
</select>
运行此代码:
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].value;
将使strUser为2.如果你真正想要的是test2,那么这样做:
var e = document.getElementById("ddlViewBy");
var strUser = e.options[e.selectedIndex].text;
哪个会使strUser成为test2