简体   繁体   English

jQuery 按值选择选项元素

[英]jQuery select option elements by value

I have a select element wrapped by a span element.我有一个由 span 元素包裹的 select 元素。 I am not allowed to use the select id but I am allowed to use the span id.我不允许使用 select id,但我可以使用 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.我正在尝试编写一个 javascript/jquery 函数,其中输入是一个数字 i,它是 select 选项的值之一。 The function will turn the relevant option to 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!谢谢!

Here's the simplest solution with a clear selector:这是带有明确选择器的最简单的解决方案:

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

使用 jQuery > 1.6.1 应该更好地使用这种语法:

$('#span_id select option[value="' + some_value + '"]').prop('selected', true);

Just wrap your option in $(option) to make it act the way you want it to.只需将您的选项包装在 $(option) 中,使其按您希望的方式运行。 You can also make the code shorter by doing您还可以通过执行使代码更短

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

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/

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

here is the fiddle http://jsfiddle.net/hRFYF/这是小提琴http://jsfiddle.net/hRFYF/

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这将获取值

function select_option(index)
{
    var optwewant;
    for (opts in $('#span_id').children('select'))
    {
        if (opts.value() = index)
        {
            optwewant = opts;
            break;
        }
    }
    alert (optwewant);
}
$("#h273yrjdfhgsfyiruwyiywer").children('[value="' + i + '"]').prop("selected", true);

You can change with simple javascript您可以使用简单的 javascript 进行更改

 document.querySelector('#h273yrjdfhgsfyiruwyiywer').value='4'
 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM