【问题标题】:Pass value of one dropdown to another using Multiple jquery .ajax() calls使用多个 jquery .ajax() 调用将一个下拉列表的值传递给另一个
【发布时间】:2014-09-18 19:28:04
【问题描述】:

我对 jquery 不太熟悉,但在这里我正在尝试这样做:

我正在尝试将一个下拉列表的值传递给另一个下拉列表,这是 jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    import="java.sql.*,com.connection.DBConnection;"%>
<!DOCTYPE html>
<html>
<head>
<title>Country</title>
<style>
.formContainer {
    margin-left: auto;
    margin-right: auto;
    width: 800px;
    border-radius: 15px;
    background-color: #75D1FF;
    text-align: center;
    padding-top: 5px;
    padding-bottom: 5px;
} 
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="Country.js"></script> 
</head>
<body>
    <div class="formContainer">
        <form action="SampleServlet" method="get">
            <fieldset>
                <label>Country :</label> <select id="countryid" name="country">
                    <option value='select'>Select</option>
                    <%
                        Connection con = DBConnection.getConnection();
                        Statement st = con.createStatement();
                        String sql = "select * from country";
                        ResultSet rs = st.executeQuery(sql);
                        while (rs.next()) {
                    %>
                    <option value='<%=rs.getString(2)%>'><%=rs.getString(2)%></option>
                    <%
                        }
                        rs.close();
                    %>
                </select><br />

                <div class="Stateselect">
                <select id="Stateid">
                </select>       
                </div><br /><br />

                <div class="Cityselect">
                <select id="Cityid" onchange="loadXMLDoc()">
                </select>
                </div><br /><br />

                <div class="Areaselect">
                <select id="Areaid"></select>
                </div><br /><br />

                <div class="buttonsDiv">
                    <button type="submit">Submit</button>
                    <button type="reset">Reset</button>
                </div>

            </fieldset>
        </form>
    </div>
</body>
</html>

在这里,根据我选择的国家/地区,应显示州,取决于州的城市和来自城市的地区。我在这里使用了三个 servlet 类,即 CountryServlet、StateServlet 和 CityServlet 这是我正在使用的 js 文件:

$(document).ready(function() {
    $(".Stateselect,.Cityselect,.Areaselect").hide();
    $("#countryid").change(function() {
        var country = document
        .getElementById("countryid").value;
        if (country != 'select') {
            $.ajax({

                url : "ActionServlet?country="+ country,
                method : "GET",
                type : "html",
                success : function(
                        result) {
                    $(".Stateselect").html(result);
                    $(".Stateselect").show(700);
                }
            });
        } else {
            $(".Stateselect").hide(700);
        };
    });

    if(document.getElementById("Stateid").style.display != "none")
    {
        $('#Stateid').change(function() {
            var country = document.getElementById("country").value;
            var state = document.getElementById("Stateid").value;
            if (country != 'select' && state != null) {
                $.ajax({
                    url : "StateServlet?country="+ country+ "&state="+ state,
                    method : "GET",
                    type : "html",
                    success : function(result) {
                        $(".Cityselect").html(result);
                        $(".Cityselect").show(700);
                    }
                });
            } else {
                $(".Cityselect").hide(700);
            };
        });
    }

    if(document.getElementById("Cityid").style.display != "none") {
        $('#Cityid').change(function() {
            var country = document.getElementById("country").value;
            var state = document.getElementById("Stateid").value;
            var city = document.getElementById("Cityid").value;
            if ((country != 'select' && state != 'select') && city != 'select') {
                $.ajax({
                    url : "CityServlet?country="+ country + "&state=" + state + "&city="+ city,
                    // SampleServlet?country=India&state=Telangana&city=Hyderabad&area=Madhapur
                    method : "GET",
                    type : "html",
                    success : function(result) {
                        $(".Areaselect").html(result);
                        $(".Areaselect").show(700);
                    }
                });
            } else {
                $(".Areaselect").hide(700);
            };
        });
    }; 
});

问题是,只有第一个 ajax 调用有效。其余两个都没有。请帮忙。提前感谢

【问题讨论】:

    标签: javascript jquery ajax jsp


    【解决方案1】:

    您在 js 代码的第一行隐藏了所有带有下拉菜单的 div

    $(".Stateselect,.Cityselect,.Areaselect").hide();
    

    然后您将根据 div 是否隐藏这一事实来绑定更改事件。

    if(document.getElementById("Cityid").style.display != "none") {
    }
    

    由于 div 被隐藏,代码不会被执行,更改事件也不会被注册。 我建议重新访问您已测试显示属性的 if 条件,如 if(document.getElementById("Cityid").style.display != "none") 删除这些,代码将起作用。

    更新:

    由于只有在 div 可见的情况下才需要运行代码,所以必须更改以下内容

    if(document.getElementById("Cityid").style.display != "none") {
            $('#Cityid').change(function() {
    

    进入

    $('#Cityid').change(function() {
      if(document.getElementById("Cityid").style.display != "none") {
    

    这样,更改事件总是被绑定,但代码只有在 div 可见时才会被执行。

    一个下拉菜单的完整代码

    $('#Cityid').change(function () {
        if (document.getElementById("Cityid").style.display != "none") {
            var country = document.getElementById("country").value;
            var state = document.getElementById("Stateid").value;
            var city = document.getElementById("Cityid").value;
            if ((country != 'select' && state != 'select') && city != 'select') {
                $.ajax({
                    url: "CityServlet?country=" + country + "&state=" + state + "&city=" + city,
                    // SampleServlet?country=India&state=Telangana&city=Hyderabad&area=Madhapur
                    method: "GET",
                    type: "html",
                    success: function (result) {
                        $(".Areaselect").html(result);
                        $(".Areaselect").show(700);
                    }
                });
            } else {
                $(".Areaselect").hide(700);
            };
        }
    });
    

    【讨论】:

    • 这就是重点。当国家下拉菜单更改时,将显示州下拉菜单。如果显示样式不是none,那么只有代码可以工作。
    • @Karthik 你在Stateselect, stateidCityselect, cityid.... 等中的ID 有什么错误吗?你隐藏了所有类(.Stateselect,.Cityselect,.Areaselect) 并与ID 进行比较(Stateid....etc)
    猜你喜欢
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-27
    • 1970-01-01
    相关资源
    最近更新 更多