让JS从HTML中读取选择<select>的形式
Getting JS to read the selection from a HTML <select> form
我正试图写一段JS代码,将从HTML <select>
表单中获得选定的选项,阅读它,然后根据选项是什么给我一些东西。
HTML代码如下所示:
<select id="mainhand" name="Main hand" onchange="getmh()">
<option value="Sword" selected> Sword </option>
<option value="Axe"> Axe </option>
<option value="Mace"> Mace </option>
</select>
<p id="mhchoice">choice</p>
<p id="mhdesc">description</p>
<p id="mhcost">cost</p>
和JS像这样:
function getmh(option){
if (option == "Sword"){
document.getElementById('mhchoice').innerHTML = 'Sword';
document.getElementById('mhdesc').innerHTML = swordDesc;
document.getElementById('mhcost').innerHTML = swordPrice;
document.getElementById('ProductImage').src = 'sword.png';
}
if (option == "Axe"){
document.getElementById('mhchoice').innerHTML = 'Axe';
document.getElementById('mhdesc').innerHTML = axeDesc;
document.getElementById('mhcost').innerHTML = axePrice;
document.getElementById('ProductImage').src = 'axe.png';
}
if (option == "Mace"){
document.getElementById('mhchoice').innerHTML = 'Mace';
document.getElementById('mhdesc').innerHTML = maceDesc;
document.getElementById('mhcost').innerHTML = macePrice;
document.getElementById('ProductImage').src = 'mace.png';
}
}
我已经被困在这个问题上将近两个小时了,我尝试了很多方法,但到目前为止似乎都没有效果。
正如上面注释中提到的@nnnnnn,您只需将选定的值this.value
传递给函数:
<select id="mainhand" name="Main hand" onchange="getmh(this.value)">
希望对你有帮助。
function getmh(option){
if (option == "Sword"){
document.getElementById('mhchoice').innerHTML = 'Sword';
document.getElementById('mhdesc').innerHTML = swordDesc;
document.getElementById('mhcost').innerHTML = swordPrice;
document.getElementById('ProductImage').src = 'sword.png';
}
if (option == "Axe"){
document.getElementById('mhchoice').innerHTML = 'Axe';
document.getElementById('mhdesc').innerHTML = axeDesc;
document.getElementById('mhcost').innerHTML = axePrice;
document.getElementById('ProductImage').src = 'axe.png';
}
if (option == "Mace"){
document.getElementById('mhchoice').innerHTML = 'Mace';
document.getElementById('mhdesc').innerHTML = maceDesc;
document.getElementById('mhcost').innerHTML = macePrice;
document.getElementById('ProductImage').src = 'mace.png';
}
}
<select id="mainhand" name="Main hand" onchange="getmh(this.value)">
<option value="Sword" selected> Sword </option>
<option value="Axe"> Axe </option>
<option value="Mace"> Mace </option>
</select>
<p id="mhchoice">choice</p>
<p id="mhdesc">description</p>
<p id="mhcost">cost</p>
我认为你是在走长路。你可以试试这样做。
<select id="myValue" onchange="CheckValue()">
<option value="1">Uno</option>
<option value="2">Dos</option>
<option value="3">Tres</option>
</select>
<script>
function CheckValue() {
var value = document.getElementById("myValue").value;
switch (value) {
// all your conditions
default:
//
}
}
</script>
相关文章:
- 如何将具有相同功能的两个select html标签的两个JS组合在一起
- 如何在angular js中的select选项中获取所选项目id
- 如何将select查询传递给js
- 使用挖空.js如何刷新 Select 元素中的可观察数组
- 敲除js试图在foreach内部使用select
- Node.js-函数不返回select from表中的数据
- Bootstrap-select.js-选择一个选项后,整个选择将消失
- JS:如果select是默认值,则禁用复选框
- d3.js用相同的select选项控制两个图
- Ember.js 如何使用视图观察选区更改 Ember.Select multiple=true
- 如何在日期选择器 jquery 的 Select 事件上调用 js 函数
- Angular js 监视带有 ng-repeat 的 Select 标签上的对象数组
- 从级联 SELECT 元素更改事件调用外部 JS 函数
- 如何将 select 中的文本与 js 中的数组匹配
- Node.js中的 Mysql select 子句
- 使用bootstrap-select.js根据第一个下拉列表更改第二个下拉列表的选项
- HTML SELECT JS onchange()禁用注释框
- 无法让jquery.multi .select.js验证并将选中的复选框传递给php
- select .js关于无结果插件和允许默认链接行为的问题
- 用php从数据库回显值到select .js