简体   繁体   English

得到选择选择框文本

[英]getting selected select box text

i am trying to use like 我正在尝试使用像

var modelLength     = document.getElementsByName("FK_BrandID")[0].text;

when i try to alert this it prints only undefined.. is this not proper..? 当我尝试提醒它时,它仅打印未定义的..这是不正确的..?

I think this is what you're after: 我想这就是你所追求的:

var sel = document.getElementsByName("FK_BrandID")[0];
var modelLength = sel.options[sel.selectedIndex].text;

You can give it a try here 你可以在这里尝试一下

You can do like this: 你可以这样做:

var index = document.getElementById("FK_BrandID").selectedIndex;
var value = document.getElementById("FK_BrandID").options[index].text;
alert(value);

That will give you the text of selected option in the select box. 这将在选择框中为您提供所选选项的文本。 To get the value of select box instead, use value 要获取选择框的值,请使用value

var value = document.getElementById("FK_BrandID").value;
alert(value);

AFAIK, no element has a text property. AFAIK,没有元素具有text属性。 Perhaps you are looking for value ? 也许您在寻找value

Everything you read above is correct, the problem in your code was, the options word was missing. 您上面阅读的所有内容都是正确的,代码中的问题是,缺少选项词。 with this your code should work: 与此您的代码应该工作:

var modelLength = document.getElementsByName("FK_BrandID")**.options**[0].text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM