[英]Can't get the value of HTML select in AJAX response
There are two steps on my code. 我的代码有两个步骤。 First step a user fills some fields and then data is submitted by ajax to server. 第一步,用户填写一些字段,然后将数据由Ajax提交到服务器。 Ajax returns a HTML select input that user must choose a value. Ajax返回一个HTML选择输入,用户必须选择一个值。 This is the second step. 这是第二步。
The problem is, when I try to get the value of select in javascript, it shows me null. 问题是,当我尝试获取javascript中select的值时,显示为null。
The code I use to get select value works in normal situation. 我用来获取选择值的代码在正常情况下有效。 But when select is retrieved by ajax, this problem occurs. 但是当用ajax检索select时,就会出现此问题。
Code to get select value 获取选择值的代码
var e = document.getElementById("ordernum");
var num = e.options[e.selectedIndex].value;
Here's an example HTML file showing how to dynamically generate a select element and get the value back from it on its onchange event via event.target.value: 这是一个示例HTML文件,该文件显示了如何动态生成select元素并通过event.target.value在其onchange事件上从中获取值:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
const GameDifficulty = {
EASY:1,
MEDIUM:2,
HARD:3,
INSANE:4
};
function init(event) {
console.log(event);
var body = document.getElementById('body');
var select = document.createElement('select');
select.id = 'selDifficulty';
for (var diff in GameDifficulty) {
var option = document.createElement('option');
option.value = GameDifficulty[diff];
option.innerHTML = diff;
select.appendChild(option);
}
select.onchange = test;
body.appendChild(select);
console.log(body);
}
function test(event) {
console.log(event.target.value);
}
window.onload = init;
</script>
</head>
<body id="body">
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.