jQuery select option elements by value

前端 未结 8 1087
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 11:50

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 functi

相关标签:
8条回答
  • 2020-12-04 12:18

    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

    0 讨论(0)
  • 2020-12-04 12:21

    You can use .val() to select the value, like the following:

    function select_option(i) {
        $("#span_id select").val(i);
    }
    

    Here is a jsfiddle: https://jsfiddle.net/tweissin/uscq42xh/8/

    0 讨论(0)
  • 2020-12-04 12:22

    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")
    
    0 讨论(0)
  • 2020-12-04 12:24
    $("#h273yrjdfhgsfyiruwyiywer").children('[value="' + i + '"]').prop("selected", true);
    
    0 讨论(0)
  • 2020-12-04 12:26

    Here's the simplest solution with a clear selector:

    function select_option(i) {
      return $('span#span_id select option[value="' + i + '"]').html();
    }
    
    0 讨论(0)
  • 2020-12-04 12:27

    With jQuery > 1.6.1 should be better to use this syntax:

    $('#span_id select option[value="' + some_value + '"]').prop('selected', true);
    
    0 讨论(0)
提交回复
热议问题