我正在尝试制作一个选择表单,根据所选的选择字段,我希望显示不同的信息。以下是我想表达它的方式:
<form id="myForm">
<select name="options" form="myForm">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
</form>
<!-- The following will be invisible until selection choice is made -->
<h1>option.name</h1>
<p>option.info</p>
然后,在JSON中,我希望商店有以下内容。我使用它作为处理程序只是为了测试它:
<script>
var data = {
"option1" : {
"name" : "Option 1",
"info" : "I'm glad you've chosen Option 1"
},
"option2" : {
"name" : "Option 2",
"info" : "Not a bad choice, either"
}
};
var myForm = document.getElementById('options');
myForm.addEventListener("change", function() {
val = this.value;
alert(data[val][name]);
}, false);
})();
</script>
警报的输出为空。