jQuery根据值选择选项元素。

时间:2022-11-27 19:36:57

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。该函数将把相关选项转换为selected。

<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!

谢谢!

7 个解决方案

#1


126  

Here's the simplest solution with a clear selector:

下面是最简单的选择器:

function select_option(i) {
  return $('span#span_id select option[value="' + i + '"]').html();
}

#2


28  

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

只要用$(选项)包装你的选项,让它按照你想要的方式运行。您还可以通过这样做使代码更短

$('#span_id > select > option[value="input your i here"]').attr("selected", "selected")

#3


26  

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);

#4


6  

options = $("#span_id>select>option[value='"+i+"']");
option = options.text();
alert(option); 

here is the fiddle http://jsfiddle.net/hRFYF/

这是小提琴http://jsfiddle.net/hRFYF/

#5


4  

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

这将获取值

#6


4  

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/

#7


1  

function select_option(index)
{
    var optwewant;
    for (opts in $('#span_id').children('select'))
    {
        if (opts.value() = index)
        {
            optwewant = opts;
            break;
        }
    }
    alert (optwewant);
}

#1


126  

Here's the simplest solution with a clear selector:

下面是最简单的选择器:

function select_option(i) {
  return $('span#span_id select option[value="' + i + '"]').html();
}

#2


28  

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

只要用$(选项)包装你的选项,让它按照你想要的方式运行。您还可以通过这样做使代码更短

$('#span_id > select > option[value="input your i here"]').attr("selected", "selected")

#3


26  

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);

#4


6  

options = $("#span_id>select>option[value='"+i+"']");
option = options.text();
alert(option); 

here is the fiddle http://jsfiddle.net/hRFYF/

这是小提琴http://jsfiddle.net/hRFYF/

#5


4  

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

这将获取值

#6


4  

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/

#7


1  

function select_option(index)
{
    var optwewant;
    for (opts in $('#span_id').children('select'))
    {
        if (opts.value() = index)
        {
            optwewant = opts;
            break;
        }
    }
    alert (optwewant);
}