我试图通过从期权中获得价值来解决问题。当我从中选择一个数字时,该数字在p标签中正确打印,并且让index也正确,但是我想在该程序的下一个函数中使用'const n'。仅出于测试目的,我创建了函数send()进行检查,我可以使用此变量,但console.log始终为NaN。我该如何解决?
<p id="p">Your number is: 0</p>
<select id="select">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<button onclick="send();">SEND</button>
//JS
var selectElem = document.getElementById('select')
var pElem = document.getElementById('p')
const n = selectElem.addEventListener('change', function() {
let index = selectElem.value;
pElem.innerHTML = 'Your number is: ' + index;
return index;
})
console.log(n);
const a = n*2;
function send(){
console.log('-----');
console.log(n);
console.log(a);
}
//JS
var selectElem = document.getElementById('select')
var pElem = document.getElementById('p')
var obj ={
n:'weqwe'
}
function setN(n){
obj.n =n
}
selectElem.addEventListener('change', function(obj) {
let index = selectElem.value;
setN( selectElem.value);
pElem.innerHTML = 'Your number is: ' + index;
return index;
})
function send(){
console.log(obj.n)
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p id="p">Your number is: 0</p>
<select id="select">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<button onclick="send();">SEND</button>
</body>
</html>