在Javascript中设置Select选项的值

时间:2021-11-24 02:06:29

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>

3 个解决方案

#1


6  

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

我有一个ajax响应数组,我想把这个数组放在一个标签中。

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

为什么不直接将数组中的选项添加到select中,然后设置所选的值,如下所示:

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 :

#2


1  

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/

访问现场演示:http://jsfiddle.net/y4B68/

thank you

#3


0  

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.

对不起,我错过了你在ajax回调中做到了。我也在我的演示中更新了。

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.

为什么不添加新选项而不是在选项中添加值。

#1


6  

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

我有一个ajax响应数组,我想把这个数组放在一个标签中。

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

为什么不直接将数组中的选项添加到select中,然后设置所选的值,如下所示:

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 :

#2


1  

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/

访问现场演示:http://jsfiddle.net/y4B68/

thank you

#3


0  

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.

对不起,我错过了你在ajax回调中做到了。我也在我的演示中更新了。

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.

为什么不添加新选项而不是在选项中添加值。