jquery 选择如果 option.value 等于某值,则标记为已选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8572545/
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
jquery selection if option.value equal something, mark a selected
提问by Giberno
I have some question about jquery selection. In my case, how to match if the option.value
equal something, mark a selected
for it. Online code here
我有一些关于 jquery 选择的问题。在我的情况下,如果option.value
相等,如何匹配,selected
为它标记一个。在线代码在这里
repeat code again. It caused Uncaught TypeError: Object #<HTMLOptionElement> has no method 'val'
, how to work as my hope? Thanks.
再次重复代码。它引起了Uncaught TypeError: Object #<HTMLOptionElement> has no method 'val'
,我希望如何工作?谢谢。
<script type="text/javascript">
$(document).ready(function(){
var num = 3;
$("div#selection select.select option").each(function(){
if((this).val()==num){
$(this).attr("selected","selected");
}
});
});
</script>
<div id="selection">
<label>Label1:</label>
<select class="select">
<option value="1">V1</option>
<option value="2">V2</option>
<option value="3">V3</option>
</select>
<label>Label2:</label>
<select class="select">
<option value="4">U1</option>
<option value="5">U2</option>
<option value="6">U3</option>
</select>
</div>
回答by Tim S.
You made a typo
你打错字了
Instead of (this).val()
you should use $(this).val()
in your if statement. this
refers to a HTMLObject, $(this)
would refer to a jQuery object. Because the .val()
method is part of the jQuery framework, you can't use it on HTMLObjects. But I'm sure you knew that because it looks very much like a small typo.
而不是(this).val()
你应该$(this).val()
在你的 if 语句中使用。this
指的是一个 HTMLObject,$(this)
将指的是一个 jQuery 对象。因为该.val()
方法是 jQuery 框架的一部分,所以不能在 HTMLObjects 上使用它。但我相信你知道这一点,因为它看起来很像一个小错别字。
This should work:
这应该有效:
$(document).ready(function(){
var num = 3;
$("div#selection select.select option").each(function(){
if($(this).val()==num){ // EDITED THIS LINE
$(this).attr("selected","selected");
}
});
});
Edit
编辑
You could optimize your loop by adding a return false;
(break;
for vanilla loops) when you have found your element so it doesn't keep looping elements while we're already "done".
当您找到元素时,您可以通过添加return false;
( break;
for vanilla loops)来优化您的循环,这样在我们已经“完成”时它不会继续循环元素。
However, you should look at Nicola Peluchetti'sanswer for a more efficient and cleaner code.
但是,您应该查看Nicola Peluchetti 的答案,以获得更高效、更简洁的代码。
回答by Nicola Peluchetti
You have a typo and to set the option as selected you should use prop()
and not attr(). in any case you could do
您有一个错字,要将选项设置为您应该使用的选项,prop()
而不是 attr()。在任何情况下你都可以做
var num = 3;
$("div#selection select.select option[value="+num+"]").prop("selected", true);
fiddle here http://jsfiddle.net/YRBrp/
在这里小提琴http://jsfiddle.net/YRBrp/
EDIT - the typo of course is what Tim S. pointed out, you should use $(this).val()
and not (this).val()
编辑 - 当然,错字是 Tim S. 指出的,你应该使用$(this).val()
而不是(this).val()
回答by XepterX
Your problem starts here
你的问题从这里开始
$("div#selection select.select option").each(function(){ if((this).val()==num){
$("div#selection select.select option").each(function(){ if( (this).val()==num){
change it to
将其更改为
$("div#selection select.select").each(function(){
if($(this).val()==num){
and all your problem solved. You might want to change the .each to .change if you are thinking of having the script triggered everytime the selection is changed.
你所有的问题都解决了。如果您考虑在每次更改选择时触发脚本,您可能希望将 .each 更改为 .change。
回答by Robin Michael Poothurai
try below
试试下面
$("div#selection select.select").val(num);
if your select box is multiple select, you can try
如果您的选择框是多选,您可以尝试
$("div#selection select.select option").each(function(){
if($(this).val()==num){
$(this).attr("selected", true);
}
});
回答by picknick
The first time where you refer to this
it is not a jQuery object it is a DOM object and a DOM object doesn't have the val()
method. You should either use this.value
och $(this).val()
try this:
第一次引用this
它时不是 jQuery 对象,而是 DOM 对象,而 DOM 对象没有该val()
方法。你应该使用this.value
och$(this).val()
试试这个:
$("div#selection select.select option").each(function(){
if($(this).val()==num){
$(this).attr("selected","selected");
}
});
回答by Umesh Patil
Your code works well. Just update 'this' with $(this). If there are no any other select dropdowns you can omit 'div#selection select.' like below.
您的代码运行良好。只需用 $(this) 更新“this”。如果没有任何其他选择下拉列表,您可以省略“div#selection select”。像下面。
$(document).ready(function(){
var num = 3;
$("select option").each(function(){
if($(this).val()==num){
$(this).attr("selected","selected");
}
});
});