如何使用JS打印HTML中下拉框中选定的值 [英] How to print the value selected in a drop-down box in HTML using JS

查看:187
本文介绍了如何使用JS打印HTML中下拉框中选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个这样的网页:

 < html> 
< head>
< title> Bug UI< / title>
< / head>

< body>

< script>
函数myfunc()
{
//这里有什么?
}
< / script>
< form>
< select name =parameters>
< option value =param1> Param 1< / option>
< option value =param2> Param 2< / option>
< option value =param3> Param 3< / option>
< option value =param4> Param 4< / option>
< option value =param5> Param 5< / option>
< / select>
< input type =buttononclick =myfunc()value =Submit>
< / form>
< / body>
< / html>

当我从框中选择一个值(比如参数1)时,它会显示一个下拉框并点击提交,我需要打印该值(在这种情况下为参数1)。如何实现这一点?

解决方案

  var s = document.getElementsByName('parameters') [0]; 
var text = s.options [s.selectedIndex] .text;


I am designing a web page which goes like this:

<html>
<head>
<title>Bug UI</title>
</head>

<body>

<script>
    function myfunc()
    {
    //what goes here??
    }
</script>
<form>
    <select name = "parameters">
        <option value = "param1">Param 1</option>
        <option value = "param2">Param 2</option>
        <option value = "param3">Param 3</option>
        <option value = "param4">Param 4</option>
        <option value = "param5">Param 5</option>
    </select>
    <input type = "button" onclick = "myfunc()" value = "Submit">
</form>
</body>
</html>

It displays a drop-down box, when I select a value (say Param 1) from the box and click "Submit", I need to print the value (Param 1 in this case). How to achieve this?

解决方案

var s = document.getElementsByName('parameters')[0];
var text = s.options[s.selectedIndex].text;

这篇关于如何使用JS打印HTML中下拉框中选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆