最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ajax - Set value of Select option in Javascript - Stack Overflow

programmeradmin2浏览0评论

Is there any function to set value of a <select> tag option? I have an ajax response array and I want to put this array in a <select> tag.

This is my code :

$.ajax({
    type: "GET",
    url: "http://.......api/1/AbsenceType",
    dataType: "json", 
    success: function (data) {
        // It doesn't work
        var ind = document.getElementById('mySelect').selectedIndex;
        document.getElementById('mySelect').options.value="2";//
    }
})

And my select tag is :

<select id=mySelect name="mySelect" required="required" data-mini ="true" onchange="myFunction3();"/>
    <option id ="type" value=""></option>
</select>

Is there any function to set value of a <select> tag option? I have an ajax response array and I want to put this array in a <select> tag.

This is my code :

$.ajax({
    type: "GET",
    url: "http://.......api/1/AbsenceType",
    dataType: "json", 
    success: function (data) {
        // It doesn't work
        var ind = document.getElementById('mySelect').selectedIndex;
        document.getElementById('mySelect').options.value="2";//
    }
})

And my select tag is :

<select id=mySelect name="mySelect" required="required" data-mini ="true" onchange="myFunction3();"/>
    <option id ="type" value=""></option>
</select>
Share Improve this question edited Jul 20, 2014 at 9:29 Scott 21.5k8 gold badges66 silver badges72 bronze badges asked Jul 20, 2014 at 9:22 Sereen starSereen star 2212 gold badges3 silver badges9 bronze badges 4
  • what do you mean ? document.getElementById('mySelect').value="2"; – Sereen star Commented Jul 20, 2014 at 9:27
  • do you mean you want to change the value of the option tag? – Mohammad Aftab Uddin Commented Jul 20, 2014 at 9:30
  • 1 i want to add value to option tag ,, it is empty and i want to fill it with the array from ajax response . – Sereen star Commented Jul 20, 2014 at 9:33
  • Use $("#type").attr("value", 2); however it just only assigns the value to 1 option tag that has the id "type", for multiple options, you would require that to generate as demonstrated in the example of Shail – Mohammad Aftab Uddin Commented Jul 20, 2014 at 9:42
Add a comment  | 

3 Answers 3

Reset to default 11

I have an ajax response array and I want to put this array in a tag.

Why don't you just add the options from the array to the select, then set the selected value, like this :

Start with an empty select :

<select id="mySelect" name="mySelect">

</select>

Then use this function as a callback :

var callback = function (data) {

    // Get select
    var select = document.getElementById('mySelect');

    // Add options
    for (var i in data) {
        $(select).append('<option value=' + data[i] + '>' + data[i] + '</option>');
    }

    // Set selected value
    $(select).val(data[1]);
}

Demo :

http://jsfiddle.net/M52R9/

See :

  • Adding options to a select using Jquery/javascript
  • Set selected option of select box.

try this...

<select>
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>`
</select>

$("select").val("3");

visit live demo here: http://jsfiddle.net/y4B68/

thank you

Are you looking for something like that -

DEMO

You can add option like this -

$(function(){
$("#mySelect").html('<option value="2">2</option>');
});

Sorry i just missed that you are doing it in ajax callback. i have also updated in my demo.

you can also this too-

document.getElementById("mySelect").innerHTML = '<option value="2">2</option>';

Why don't you add new option instead of adding a value in option.

发布评论

评论列表(0)

  1. 暂无评论