Javascript jQuery按值选择选项元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7290950/
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 select option elements by value
提问by rapt
I have a select element wrapped by a span element. I am not allowed to use the select id but I am allowed to use the span id. I am trying to write a javascript/jquery function in which the input is a number i, which is one of the values of the select's options. The function will turn the relevant option to selected.
我有一个由 span 元素包裹的 select 元素。我不允许使用 select id,但我可以使用 span id。我正在尝试编写一个 javascript/jquery 函数,其中输入是数字 i,这是选择选项的值之一。该功能会将相关选项变为选中状态。
<span id="span_id">
<select id="h273yrjdfhgsfyiruwyiywer" multiple="multiple">
<option value="1">cleaning</option>
<option value="2">food-2</option>
<option value="3">toilet</option>
<option value="4">baby</option>
<option value="6">knick-knacks</option>
<option value="9">junk-2</option>
<option value="10">cosmetics</option>
</select>
</span>
I wrote something as follows (this does not completely work, which is why I am posting this question):
我写的东西如下(这并不完全有效,这就是我发布这个问题的原因):
function select_option(i) {
options = $('#span_id').children('select').children('option');
//alert(options.length); //7
//alert(options[0]); //[object HTMLOptionElement]
//alert(options[0].val()); //not a jquery element
//alert(options[0].value); //1
//the following does not seem to work since the elements of options are DOM ones not jquery's
option = options.find("[value='" + i + "']");
//alert(option.attr("value")); //undefined
option.attr('selected', 'selected');
}
Thanks!
谢谢!
回答by jonathan.cone
Here's the simplest solution with a clear selector:
这是带有明确选择器的最简单的解决方案:
function select_option(i) {
return $('span#span_id select option[value="' + i + '"]').html();
}
回答by damoiser
With jQuery > 1.6.1 should be better to use this syntax:
使用 jQuery > 1.6.1 应该更好地使用以下语法:
$('#span_id select option[value="' + some_value + '"]').prop('selected', true);
回答by Adrian Rodriguez
Just wrap your option in $(option) to make it act the way you want it to. You can also make the code shorter by doing
只需将您的选项包装在 $(option) 中,使其按照您希望的方式运行。您还可以通过执行以下操作来缩短代码
$('#span_id > select > option[value="input your i here"]').attr("selected", "selected")
回答by Rafay
options = $("#span_id>select>option[value='"+i+"']");
option = options.text();
alert(option);
here is the fiddle http://jsfiddle.net/hRFYF/
回答by tom
You can use .val() to select the value, like the following:
您可以使用 .val() 来选择值,如下所示:
function select_option(i) {
$("#span_id select").val(i);
}
Here is a jsfiddle: https://jsfiddle.net/tweissin/uscq42xh/8/
这是一个 jsfiddle:https://jsfiddle.net/tweissin/uscq42xh/8/
回答by Arijit Ghosh
To get the value just use this:
要获得该值,只需使用以下命令:
<select id ="ari_select" onchange = "getvalue()">
<option value = "1"></option>
<option value = "2"></option>
<option value = "3"></option>
<option value = "4"></option>
</select>
<script>
function getvalue()
{
alert($("#ari_select option:selected").val());
}
</script>
this will fetch the values
这将获取值
回答by Hogan
function select_option(index)
{
var optwewant;
for (opts in $('#span_id').children('select'))
{
if (opts.value() = index)
{
optwewant = opts;
break;
}
}
alert (optwewant);
}
回答by rsc
$("#h273yrjdfhgsfyiruwyiywer").children('[value="' + i + '"]').prop("selected", true);