我的onClick事件没有设置“;selectedValue"用户选择的选项中的变量
My onClick event is not setting "selectedValue" variable from user selected option?
我有一个用城市名称填充的下拉框。当用户点击/选择其中一个城市名称时,他们点击的值(或选项)将被设置为变量"selectedValue"。我的onClick事件似乎无法正常工作,尽管我不确定到底出了什么问题。
var selectValues = new Array(["Auckland"],["Christchurch"],["Dunedin"],["Hamilton"],["Tauranga"],["Wellington"],["Nelson"]);
console.log("Pre-set select menu towns: " + selectValues);
//creates _dropList as a select menu object with an ID and Class Name
var _dropList = document.createElement("select"); {
_dropList.id = "selectmenu";
_dropList.className = "selectmenu";
}
//loops through the array "selectValues" and adds each value (town name) to the selectmenu as an option
for ( var i = 0; i < selectValues.length; i++ ) {
//creates a variable "_options"
var _options = document.createElement("option");
//"_options" is equal to the values of "selectValues" ("selectValues" values are added to the select menu)
_options.value = selectValues[i];
//debugging
console.log("Select Menu Options: " + _options.value +" - Array Value: " + i);
_options.innerHTML = selectValues[i];
//appends the "selectValues" values (which are now equal to "_options" to the select menu)
_dropList.appendChild(_options);
//on click of the select menu option...
_options.onClick = function() {
//checks the value of the select menu and then
var check = _dropList.selectedIndex;
//make a variable called "selectedValue" and assign it the value of the users chosen option
var selectedValue = selectValues[check];
//sends the value of "selectedValue" to the _checkNewTown function
_checkNewTown(selectedValue);
}
}
选项标签上的onclick事件在大多数版本的IE、Safari和Chrome上都会失败。
请参阅此问题->onclick上的选项标签不适用于IE和chrome
var selectValues = [
["Auckland"],
["Christchurch"],
["Dunedin"],
["Hamilton"],
["Tauranga"],
["Wellington"],
["Nelson"]
];
console.log("Pre-set select menu towns: " + selectValues);
//creates _dropList as a select menu object with an ID and Class Name
var _dropList = document.createElement("select");
_dropList.id = "selectmenu";
_dropList.className = "selectmenu";
_dropList.addEventListener('change', function() {
_checkNewTown(this.value);
});
//loops through the array "selectValues" and adds each value (town name) to the selectmenu as an option
for (var i = 0; i < selectValues.length; i++) {
//creates a variable "_options"
var _options = document.createElement("option");
//"_options" is equal to the values of "selectValues" ("selectValues" values are added to the select menu)
_options.value = selectValues[i];
//debugging
console.log("Select Menu Options: " + _options.value + " - Array Value: " + i);
_options.innerHTML = selectValues[i];
//appends the "selectValues" values (which are now equal to "_options" to the select menu)
//on click of the select menu option...
_dropList.appendChild(_options);
document.body.appendChild(_dropList);
}
function _checkNewTown(i) {
document.getElementById('data').innerText = i;
}
<div id='data'></div>
通过addEventListener()
附加函数
//with onClick event
_dropList.addEventListener('click', function() {
// code here for after select
);
//with onChange event
_dropList.addEventListener('change', function() {
// code here for after select
);
var selectValues = new Array(["Auckland"],["Christchurch"],["Dunedin"],["Hamilton"],["Tauranga"],["Wellington"],["Nelson"]);
console.log("Pre-set select menu towns: " + selectValues);
//creates _dropList as a select menu object with an ID and Class Name
var _dropList = document.createElement("select"); {
_dropList.id = "selectmenu";
_dropList.className = "selectmenu";
}
//loops through the array "selectValues" and adds each value (town name) to the selectmenu as an option
for ( var i = 0; i < selectValues.length; i++ ) {
//creates a variable "_options"
var _options = document.createElement("option");
//"_options" is equal to the values of "selectValues" ("selectValues" values are added to the select menu)
_options.value = selectValues[i];
//debugging
console.log("Select Menu Options: " + _options.value +" - Array Value: " + i);
_options.innerHTML = selectValues[i];
//appends the "selectValues" values (which are now equal to "_options" to the select menu)
_dropList.appendChild(_options);
document.body.appendChild(_dropList);
//on click of the select menu option...
_dropList.addEventListener('click', function() {
//checks the value of the select menu and then
var check = _dropList.selectedIndex;
//make a variable called "selectedValue" and assign it the value of the users chosen option
var selectedValue = selectValues[check];
//sends the value of "selectedValue" to the _checkNewTown function
_checkNewTown(selectedValue);
}
);
}
function _checkNewTown(val){
console.log('selcted val ' + val);
}
<html>
<body>
</body>
</html>
相关文章:
- "“;变量未引用正确的对象
- "可以't查找变量:cordova”;
- "可以't查找变量:window“;在suds.js中
- 得到"全局未定义“;全局变量定义清楚时出错
- 传递带有符号“”的字符串变量'"到函数
- "可变变量可从闭包“”访问;在传递给Array.prototype.every的函数中
- 我可以't克服这个错误“;未定义的局部变量或方法`f'"任何原因
- Escape”"字符,以便在jQuery中输入变量
- "SyntaxError:JSON.parse:意外字符"将多个变量从AJAX传递到PHP时出错
- 我怎么能"保存“;变量客户端,这样我就可以在其他java脚本事件中使用它
- 关于变量分配的澄清"var x=!0〃;
- 为什么应该"这个“;被分配给jQuery中的变量
- EmberJS,如何管理不同的"配置变量“;对于不同的“;环境”;
- "未定义的“;全局变量-非常奇怪
- 得到"公共变量“;在“;私有方法”;使用CoffeeScript
- Lawnchair javascript数据"得到“;不允许我分配给另一个变量
- 引用类内的变量..当“;这个"不起作用,因为我们处于函数中
- "未捕获的类型错误:无法读取属性'长度'未定义的“;在定义变量之后
- 我的onClick事件没有设置“;selectedValue"用户选择的选项中的变量
- "[对象对象]”;在警报提示中使用时替换变量字符串