如何使用 jQuery 执行此操作 - document.getElementById("selectlist").value
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1310159/
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 do this using jQuery - document.getElementById("selectlist").value
提问by tonyf
In jQuery, what is the equivalent to document.getElementById("selectlist").value
?
在 jQuery 中,什么相当于document.getElementById("selectlist").value
?
I am trying to get the value of a select list item.
我正在尝试获取选择列表项的值。
Thanks.
谢谢。
回答by James Wiseman
"Equivalent" is the word here
“等价”是这里的词
While...
尽管...
$('#selectlist').val();
...is equivalent to...
……相当于……
document.getElementById("selectlist").value
...it's worth noting that...
……值得注意的是……
$('#selectlist')
...although 'equivalent' is not the same as...
...虽然“等效”与...不同
document.getElementById("selectlist")
...as the former returns a jQuery object, not a DOM object.
...因为前者返回一个 jQuery 对象,而不是一个 DOM 对象。
To get the DOM object(s) from the jQuery one, use the following:
要从 jQuery 获取 DOM 对象,请使用以下命令:
$('#selectlist').get(); //get all DOM objects in the jQuery collection
$('#selectlist').get(0); //get the DOM object in the jQuery collection at index 0
$('#selectlist')[0]; //get the DOM objects in the jQuery collection at index 0
回答by ChaosPandion
$('#selectlist').val();
回答by RodH257
Chaos is spot on, though for these sorts of questions you should check out the Jquery Documentationonline - it really is quite comprehensive. The feature you are after is called 'jquery selectors'
混乱就在眼前,但对于这些类型的问题,您应该在线查看Jquery 文档- 它确实非常全面。您所追求的功能称为“jquery 选择器”
Generally you do $('#ID').val()
- the .afterwards can do a number of things on the element that is returned from the selector. You can also select all of the elements on a certain class and do something to each of them. Check out the documentation for some good examples.
通常你会这样做$('#ID').val()
- .afterwards 可以对从选择器返回的元素做很多事情。您还可以选择某个类上的所有元素,并对它们中的每个元素进行处理。查看文档以获取一些好的示例。
回答by sayannayas
It can be done by three different ways,though all them are nearly the same
它可以通过三种不同的方式完成,尽管它们几乎相同
Javascript way
Javascript方式
document.getElementById('test').value
Jquery way
jQuery方式
$("#test").val()
$("#test")[0].value
$("#test").get(0).value
回答by marksyzm
For those wondering if jQuery id selectors are slower than document.getElementById, the answer is yes, but not because of the preconception that it searches through the entire DOM looking for an element. jQuery does actually use the native method. It's actually because jQuery uses a regular expression first to separate out strings in the selector to check by, and of course running the constructor:
对于那些想知道 jQuery id 选择器是否比 document.getElementById 慢的人,答案是肯定的,但不是因为它在整个 DOM 中搜索一个元素的先入之见。jQuery 确实使用了本机方法。其实是因为jQuery首先使用正则表达式将选择器中的字符串分离出来进行检查,当然还要运行构造函数:
rquickExpr = /^(?:(<[\w\W]+>)[^>]*|#([\w-]*))$/
Whereas using a DOM element as an argument returns immediately with 'this'.
而使用 DOM 元素作为参数会立即返回“this”。
So this:
所以这:
$(document.getElementById('blah')).doSomething();
Will always be faster than this:
总是比这更快:
$('#blah').doSomething();
回答by Brett Ryan
In some cases of which I can't remember why but $('#selectlist').val()
won't always return the correct item value, so I use $('#selectlist option:selected').val()
instead.
在某些情况下,我不记得为什么但$('#selectlist').val()
不会总是返回正确的项目值,所以我$('#selectlist option:selected').val()
改为使用。