1

有什么方法可以组合以下 3 条 jQuery 语句?

$("#search_building").change(function () {
    var building = $("#search_building").val();
    var room = $("#search_room").val();
    var dept = $("#dept").val();
    var dataString = 'room=' + room + '&' + 'building=' + building + '&' + 'dept=' + dept;
    $.ajax({
        type: "POST",
        url: "process_building.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#search_room').html(html);
        }
    });
    $.ajax({
        type: "POST",
        url: "process_timetableMon.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#grid2_mon').html(html);
        }
    });
});

$("#search_building").blur(function () {
    var building = $("#search_building").val();
    var room = $("#search_room").val();
    var dept = $("#dept").val();
    var dataString = 'room=' + room + '&' + 'building=' + building + '&' + 'dept=' + dept;
    $.ajax({
        type: "POST",
        url: "process_building.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#search_room').html(html);
        }
    });
    $.ajax({
        type: "POST",
        url: "process_timetableMon.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#grid2_mon').html(html);
        }
    });
});

$("#search_building").ready(function () {
    var building = $("#search_building").val();
    var room = $("#search_room").val();
    var dept = $("#dept").val();
    var dataString = 'room=' + room + '&' + 'building=' + building + '&' + 'dept=' + dept;
    $.ajax({
        type: "POST",
        url: "process_timetableMon.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#grid2_mon').html(html);
        }
    });
});
4

1 回答 1

4

jQueryon支持将同一个处理程序绑定到多个事件,并且是在 jQuery 1.7+ 中使用的合适方法。

$("#search_building").on("change blur", function () {
    // your code here
});

至于您的就绪处理程序,没有真正的ready事件,并且 jQuery 在这种情况下通常会忽略选择器并在任何情况下都在 DOM 就绪时执行。您可以简单地触发blurDOM 就绪:

$(function() {
    $('#search_building').blur();
});
于 2013-02-17T01:26:12.663 回答