选择< select>的值用javascript标记。问题 [英] Selecting value of <select> tag in javascript. problem

查看:74
本文介绍了选择< select>的值用javascript标记。问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部Javascript文件,我正在尝试 alert select tag的值。

I have an external Javascript file and i am trying to alert the value of select tag.

我的< select> 代码如下所示:

<select id="vote">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>
<input type="button" value="vote" onclick="castvote();">

和Javascript(外部):

and the Javascript (which is external):

function castvote()
{
    alert(document.vote.options[document.vote.selectedIndex].value);
}

但我收到错误document.vote is undefined。

But I get the error "document.vote is undefined".

有人可以帮我解决这个问题。

Can someone help me with this.

最佳
Zeeshan

Best Zeeshan

推荐答案

如果按Id选择,你应该使用:

If selecting by Id, you should use :

function castvote() { 
   var sel = document.getElementById("vote");

   alert(sel.options[sel.selectedIndex].value); 
}

这篇关于选择&lt; select&gt;的值用javascript标记。问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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