如何通过名称获取所选元素,然后使用jQuery从下拉列表中获取所选值? [英] How do I get the selected element by name and then get the selected value from a dropdown using jQuery?
本文介绍了如何通过名称获取所选元素,然后使用jQuery从下拉列表中获取所选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以通过名称找到我的选择元素,但是我找不到与之关联的选择值.
I am able to find my select element by name, but I'm having trouble finding the selected value associated with it.
这是我的下面的代码:
<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:({}),选择器:"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?
推荐答案
您的选择器有点小,它缺少尾随的]
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 + '"]')
这篇关于如何通过名称获取所选元素,然后使用jQuery从下拉列表中获取所选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文