jQuery ajax从选择框调用 [英] jquery ajax call from select box

查看:47
本文介绍了jQuery ajax从选择框调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用jquery从选择框的onchange事件进行ajax调用?如果有人有样品 请发布代码.

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

推荐答案

以下代码基于spring mvc体系结构,

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();
});

在html内,我使用如下所示的方法来显示selectbox2值,

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>

在selectbox2中,使用ajax调用从控制器中加载值,在控制器中,我返回如下所示,

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;

这篇关于jQuery ajax从选择框调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆