javascript 如何在组合框中获取选定的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6874790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get selected value in combo box?
提问by Sandra Schlichting
If I have a form like this.
如果我有这样一个形式,这样。
<form name="myform" action="" method="POST">
<select name="mydropdown">
<option value="A">AAA</option>
<option value="B">BBB</option>
<option value="C">CCC</option>
</select>
</form>
For radio buttons I would do
对于单选按钮,我会做
var type = $(this).find('input:radio[name="ctype"]:checked').val() || '';
but this can't I get to work on combo boxes.
但这不能在组合框上工作。
How can I get the value of the selected option in a combo box?
如何获取组合框中所选选项的值?
Update
更新
It is called on a webpage with many forms, so I need the selected value from this
particular combo box.
它在具有多种形式的网页上被调用,因此我需要从this
特定组合框中选择的值。
Here is how I get the values from text boxes and radio buttons.
这是我从文本框和单选按钮获取值的方法。
$('form').live('submit', function(){
var title = this.elements.title.value;
var type = $(this).find('input:radio[name="ctype"]:checked').val() || '';
var sect = ?
...
采纳答案by leandre_b
Simply :
简单地 :
$('select[name="mydropdown"]').val();
UPDATE:
更新:
And when inside one of the forms :
当在其中一种形式中时:
$('form').live('submit', function(){
var value_of_dropdown = $(this).find('select[name="mydropdown"]').val();
...
回答by DrStrangeLove
$('select[name="mydropdown"] option:selected').val()
UPDATE:
更新:
$('form').live('submit', function(){
var value_of_dropdown = $(this).find('select[name="mydropdown"] option:selected').val();
...
});
回答by brezanac
For select boxes its enough to use val()
to get the selected value.
对于选择框,它足以val()
用于获取所选值。
$('select[name="mydropdown"]').val();
EDIT:I edited the response because I could have sworn there was an id named mydropdown:)
编辑:我编辑了回复,因为我可以发誓有一个名为mydropdown的 id :)
回答by Chakavak Behzad
Get value selected with javascript:
获取使用 javascript 选择的值:
var countsw=document.getElementById("controlid").options[document.getElement.getElementById("controlid").selectedIndex].value;
With this code you can get value selected combobox even if client-side control has this attribute runat="server"
that use in server-side.
使用此代码,即使客户端控件具有runat="server"
在服务器端使用的此属性,您也可以获得选定的组合框的值。
回答by abhijit
$('select[name="mydropdown"]').change(function() {
$('select[name="mydropdown"]').val();
});
回答by jAndy
Using jQuery, just go for it like:
使用 jQuery,就像这样:
$('select').val();