Set select option 'selected', by value

后端 未结 28 2268
攒了一身酷
攒了一身酷 2020-11-22 10:40

I have a select field with some options in it. Now I need to select one of those options with jQuery. But how can I do that when I only know the

相关标签:
28条回答
  • 2020-11-22 11:39

    For me the following did the job

    $("div.id_100").val("val2").change();
    
    0 讨论(0)
  • 2020-11-22 11:40
    • You must take it in mind the value you want to change dynamically, must be same as one present in the options you define in your HTML as it is case sensative. You can change your select box dynamically as follows,

    $("div#YOUR_ID").val(VALUE_CHANGED).change(); //value must present in options you selected otherwise it will not work

    0 讨论(0)
  • 2020-11-22 11:41

    a simple answer is, at html

    <select name="ukuran" id="idUkuran">
        <option value="1000">pilih ukuran</option>
        <option value="11">M</option>
        <option value="12">L</option>
        <option value="13">XL</option>
    </select>
    

    on jquery, call below function by button or whatever

    $('#idUkuran').val(11).change();
    

    it simple and 100% works, coz its taken from my work... :) hope its help..

    0 讨论(0)
  • 2020-11-22 11:45

    This Works well

    jQuery('.id_100').change(function(){ 
            var value = jQuery('.id_100').val(); //it gets you the value of selected option 
            console.log(value); // you can see your sected values in console, Eg 1,2,3
        });
    
    0 讨论(0)
提交回复
热议问题