使用Javascript和HTML获取选定的选项,并根据值显示字符串
Using Javascript with HTML to get a selected option and display a String based on value
抱歉,如果这个问题之前已经问过,我已经环顾四周(这个网站和其他几个)的例子和代码片段,但似乎没有工作。
我正在学习HTML课程,作业要求使用脚本检查ddl并根据所选选项显示一行文本。问题是,我在网上找到的东西似乎与我们老师提供的例子有点不同(我们使用的是Dreamweaver 2015,如果这有区别的话)
这是我从几个不同的线程中分离代码几个小时后所遇到的问题。
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Hot Buns </title>
</head>
<script type="text/javascript">
function showDetails()
{
var x= document.getElementById("slctOrder");
var val= x.options[x.selectedIndex].text;
<!-- DW only displays item(n) and len after the dot, I typed in "text" manually -->
document.forms["frmOrder"]["Details"].value = val;
<!-- I'm just trying to have it display anything at this point,
same as above, I typed "value" since DW doesn't seem to recognize this code
(this is from the teacher's example) -->
}
</script>
<body>
<p> <h2> Welcome to Hot Buns </h2> <br> <h3> Ham can we bee'f service? </h3></p>
<p> <h2> Place an order </h2> </p>
<form method="get" name="frmOder">
<select name="slctOrder" onChange="showDetails();">
<!-- I'm trying to call the function showDetails()
but neither onChange here nor onClick in the option tag seem to accomplish that -->
<option hidden selected="selected" value="0"> please select one of today's specials </option>
<option value="1" onClick="showDetails();"> Last of the Mo-jicama </option>
<option value="2" onClick="showDetails();"> Cheesus Is Born Burger </option>
<option value="3" onClick="showDetails();"> Beets of Burden Burger </option>
<option value="4" onClick="showDetails();"> Paranormal Pepper Jack-tivity Burger </option>
</select>
<output name="Details"/>
</form>
</body>
</html>
一些小改动,但这里是工作示例:
update:将所选值输出到HTML元素vs alert.
更新2:我已经改变了代码,以完全匹配你的,但修复了一些错误,使其工作。
1)表单名称"frmOder"出现了拼写错误,但在代码中,您使用了"frmOrder"。
document.forms["frmOder"]["Details"].value = val;
2)您正在尝试通过id选择元素,但id属性不存在:
document.getElementById("slctOrder");
必须更改:
<select name="slctOrder" onChange="showDetails();">
:
<select id="slctOrder" onChange="showDetails();">
3)选项标签中不需要onclick事件
我在原来的答案中切换出'output'元素的原因是因为它不是IE中支持的标签。http://www.w3schools.com/tags/tag_output.asp
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title> Hot Buns </title>
</head>
<script type="text/javascript">
function showDetails()
{
var x= document.getElementById("slctOrder");
var val= x.options[x.selectedIndex].text;
document.forms["frmOder"]["Details"].value = val;
}
</script>
<body>
<p> <h2> Welcome to Hot Buns </h2> <br> <h3> Ham can we bee'f service? </h3></p>
<p> <h2> Place an order </h2> </p>
<form method="get" name="frmOder">
<select id="slctOrder" onChange="showDetails();">
<!-- I'm trying to call the function showDetails()
but neither onChange here nor onClick in the option tag seem to accomplish that -->
<option hidden selected="selected" value="0"> please select one of today's specials </option>
<option value="1"> Last of the Mo-jicama </option>
<option value="2"> Cheesus Is Born Burger </option>
<option value="3"> Beets of Burden Burger </option>
<option value="4"> Paranormal Pepper Jack-tivity Burger </option>
</select>
<output name="Details"/>
</form>
</body>
</html>
您可以通过添加一个事件侦听器来简化标记(此处更改了部分)和代码。
标记(修订)
<form method="get" id="frmOder">
<select id="slctOrder">
<option hidden selected="selected" value="0"> please select one of today's specials </option>
<option value="1"> Last of the Mo-jicama </option>
<option value="2"> Cheesus Is Born Burger </option>
<option value="3"> Beets of Burden Burger </option>
<option value="4"> Paranormal Pepper Jack-tivity Burger </option>
</select>
<output id="Details" />
</form>
代码function showDetails(event) {
console.log(event);
// var x = document.getElementById("slctOrder");
var x = event.srcElement;// same as above but use the event
var val = x.options[x.selectedIndex].text;
// no need for above, just get the text value (assumes single select)
var t = event.srcElement.selectedOptions[0].text;
document.getElementById("Details").value = t;
}
// add event listener to select
var el = document.getElementById("slctOrder");
el.addEventListener("change", showDetails, false);
注意你仍然可以使用名称来select:(但它返回一个集合,所以第一个需要[0])
document.getElementsByName("slctOrder")[0];
示例小提琴演奏:https://jsfiddle.net/MarkSchultheiss/j28cpsg9/
相关文章:
- 高亮显示与数组字符串一起使用时文本插件中断
- 是否有任何JavaScript UI组件可以显示字符串之间的差异
- 单击按钮时显示随机字符串
- 如何显示字符串中最多 200 个字符
- Javascript使用php日期字符串显示2014年而不是2013年
- Nodejs json字符串显示为[object object]
- JavaScript cookie 输入字段字符串显示错误
- 下划线模板:“<字符串”不显示,但“<字符串”显示
- 数组中的字符串显示为未定义,尽管在几行之前成功.log控制台
- 如何使用查询字符串显示筛选后的视图
- 在Spring MVC中字符串显示YUI数据表时发生数据错误
- 打印字符串显示为未定义
- 为什么typeof字符串显示'undefined'在js
- 计算字符串中的出现次数和格式化字符串:显示结果
- 根据URL字符串显示内容
- 如何在Javascript中添加一个对象时使字符串显示为数组返回到数组
- 从单个字符串显示一系列的扑克牌图像
- regex字符串显示一个错误
- 对象的JavaScript数组-第0个元素未定义,但字符串显示对象
- AngularJS使用base64字符串显示图像