如何按名称获取所选元素,然后使用 jQuery 从下拉列表中获取所选值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11909109/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 11:03:56  来源:igfitidea点击:

How do I get the selected element by name and then get the selected value from a dropdown using jQuery?

jqueryselect

提问by dt1000

I am able to find my select element by name, but I'm having trouble finding the selected value associated with it.

我可以按名称找到我的选择元素,但我无法找到与之关联的选定值。

Here is my code below:

这是我的代码如下:

<select  name="a[b]" onchange='mySelectHandler("a[b]")'>
     <option value='Choice 1'>Choice 1</option>
     <option value='Choice 2'>Choice 2</option>

</select>

then, in my handler I am using:

然后,在我的处理程序中,我正在使用:

function mySelectHandler(name){
     var mySelect = $('select[name=' + name)
     // try to get selected value
     // alert ("selected " + mySelect.val())
     console.log("object "+ mySelect.toSource());
  }

The result of my print to the log is:

我打印到日志的结果是:

object ({length:0, prevObject:{0:({}), context:({}), length:1}, context:({}), selector:"select[name=a[b]"})

object ({length:0, prevObject:{0:({}), context:({}), length:1}, context:({}), selector:"select[name=a[b]"})

Any ideas as to how to do this?

关于如何做到这一点的任何想法?

回答by MrOBrian

Your selector is a little off, it's missing the trailing ]

你的选择器有点不对,它缺少尾随 ]

var mySelect = $('select[name=' + name + ']')

you may also need to put quotes around the name, like so:

您可能还需要在名称周围加上引号,如下所示:

var mySelect = $('select[name="' + name + '"]')

回答by Gromer

Try this:

尝试这个:

$('select[name="' + name + '"] option:selected').val();

This will get the selected value of your menu.

这将获得菜单的选定值。

回答by Shyju

Remove the onchangeevent from the HTML Markup and bind it in your document ready event

onchange从 HTML 标记中删除事件并将其绑定到您的文档就绪事件中

<select  name="a[b]" >
     <option value='Choice 1'>Choice 1</option>
     <option value='Choice 2'>Choice 2</option>
</select>?

and Script

和脚本

$(function(){    
    $("select[name='a[b]']").change(function(){
       alert($(this).val());        
    }); 
});

Working sample : http://jsfiddle.net/gLaR8/3/

工作示例:http: //jsfiddle.net/gLaR8/3/

回答by nnnnnn

MrOBrian's answer shows why your current code doesn't work, with the missing trailing ]and quotes, but here's an easier way to make it work:

MrOBrian 的回答显示了为什么您当前的代码不起作用,缺少尾随]和引号,但这里有一个更简单的方法来使它工作:

onchange='mySelectHandler(this)'

And then:

进而:

function mySelectHandler(el){
     var mySelect = $(el)
     // get selected value
     alert ("selected " + mySelect.val())
  }

Or better still, remove the inline event handler altogether and bind the event handler with jQuery:

或者更好的是,完全删除内联事件处理程序并使用 jQuery 绑定事件处理程序:

$('select[name="a[b]"]').change(function() {
    var mySelect = $(this);
    alert("selected " mySelect.val());
});

That last would need to be in a document.ready handler or in a script block that appears after the select element. If you want to run the same function for other selects simply change the selector to something that applies to all, e.g., allselects would be $('select'), or all with a particular class would be $('select.someClass').

最后一个需要在 document.ready 处理程序中或在 select 元素之后出现的脚本块中。如果您想为其他选择运行相同的功能,只需将选择器更改为适用于所有的内容,例如,所有选择将是$('select'),或者所有具有特定类的将是$('select.someClass')

回答by Mahesh K Vishwanathan

Try this to get value from select element by Element Name

试试这个通过元素名称从选择元素中获取值

$("select[name=elementnamehere]").val();

回答by Karthik Sundharesan

or you can simply do

或者你可以简单地做

$('select[name=a[b]] option:selected').val()

回答by haakym

To add to the answers here, ensure there's no space between the selectand [name...

要添加到此处的答案,请确保select和之间没有空格[name...

Wrong:

错误的:

'select [name=' + name + ']'
       ^

Right:

对:

'select[name=' + name + ']'

回答by ?????

instead of

代替

mySelect.toSource()

use

mySelect.val()