Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How to make ajax call from onchange event of select box using jquery? If anyone has a sample, please post the code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
972 views
Welcome To Ask or Share your Answers For Others

1 Answer

the below code is based on spring mvc architecture,

$('#selectbox1').change(function() {
    var data = "";
    $.ajax({
        type:"GET",
        url : "controller mapping",
        data : "selectbox1_selectedvalue="+$(this).val(),
        async: false,
        success : function(response) {
            data = response;
            return response;
        },
        error: function() {
            alert('Error occured');
        }
    });
    var string = data.message.split(",");
    var array = string.filter(function(e){return e;});
    var select = $('selectbox2');
    select.empty();
    $.each(array, function(index, value) {          
        select.append(
                $('<option></option>').val(value).html(value)
            );
    });
        $('#selectbox2').show();
});

inside the html, i use like below to display the selectbox2 values,

<tr>
    <select id="selectbox1">
        <option value="value1">value1</option><option value="value2">value2</option>
    </select>
    <select id="selectbox2"></select>
</tr>

in the selectbox2, values are loaded from a controller using ajax call, in controller i return like below,

    List<String> listvalues = listService.retrieveAll(searchTerm); // searchTerm is a selected value from selectbox1
String dummy = "";
for(int i=0; i<listvalues.size(); i++)
{
    dummy += listvalues.get(i)+",";
}
MessageValue messageValue = new MessageValue(dummy);
return messageValue;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...