jquery ajax call from select box

前端 未结 2 874
花落未央
花落未央 2020-12-20 05:33

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

2条回答
  •  心在旅途
    2020-12-20 06:12

    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(
                    $('').val(value).html(value)
                );
        });
            $('#selectbox2').show();
    });
    

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

    
        
        
    
    

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

        List listvalues = listService.retrieveAll(searchTerm); // searchTerm is a selected value from selectbox1
    String dummy = "";
    for(int i=0; i

提交回复
热议问题