0

结合以下 jQuery 语句的最佳方式是什么——它们似乎都在做同样的事情,但正在处理不同的事件?

我希望通过这样做使我的代码更有效率。

 $(document).on('change click blur', '.roomFac', function () {
        var park = $("#park2").val();
        var lecturestyle = $("#lecture_style2").val();
        var roomstructure = $("#room_structure2").val();
        var groupsize = $("#groupSize2").val();
        var facilities = "";
        $('select[name*=roomFac]').each(function () {
            facilities += $(this).val();
            facilities += ",";
        });
        var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
            'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
            'facilities=' + facilities;
        $.ajax({
            type: "POST",
            url: "process_timetableMon2.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $('#mon').html(html);
            }
        });
    });

    // search - park2

    $("#park2").on("change click blur", function () {
        var park = $("#park2").val();
        var lecturestyle = $("#lecture_style2").val();
        var roomstructure = $("#room_structure2").val();
        var groupsize = $("#groupSize2").val();
        var facilities = "";
        $('select[name^=roomFac]').each(function () {
            facilities += $(this).val();
            facilities += ",";
        });
        facilities = substring(0, facilities.length - 1)
        var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
            'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
            'facilities=' + facilities;
        alert(dataString);
        $.ajax({
            type: "POST",
            url: "process_timetableMon2.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $('#mon').html(html);
            }
        });
    });

    // search - lecturestyle2

    $("#lecturestyle2").on("change click blur", function () {
        var park = $("#park2").val();
        var lecturestyle = $("#lecture_style2").val();
        var roomstructure = $("#room_structure2").val();
        var groupsize = $("#groupSize2").val();
            var facilities = $('#roomFac').val().join( ',' );
        var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
            'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
            'facilities=' + facilities;
        $.ajax({
            type: "POST",
            url: "process_timetableMon2.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $('#mon').html(html);
            }
        });
    });

    // search - room_structure2

    $("#room_structure2").on("change click blur", function () {
        var park = $("#park2").val();
        var lecturestyle = $("#lecture_style2").val();
        var roomstructure = $("#room_structure2").val();
        var groupsize = $("#groupSize2").val();
            var facilities = $('#roomFac').val().join( ',' );
        var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
            'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
            'facilities=' + facilities;
        $.ajax({
            type: "POST",
            url: "process_timetableMon2.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $('#mon').html(html);
            }
        });
    });

    // search - groupSize2

    $("#slider2").on("change click blur", function () {
        var park = $("#park2").val();
        var lecturestyle = $("#lecture_style2").val();
        var roomstructure = $("#room_structure2").val();
        var groupsize = $("#groupSize2").val();
                var facilities = $('#roomFac').val().join( ',' );
        var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
            'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
            'facilities=' + facilities;
        $.ajax({
            type: "POST",
            url: "process_timetableMon2.php",
            data: dataString,
            cache: false,
            success: function (html) {
                $('#mon').html(html);
            }
        });
    });
4

2 回答 2

0

使用逗号:

$('#park2,#lecturestyle2,#roomstructure2,#slider2').add(document).on(/* blah blah blah*/)

由于您无法$('document')输入$('somestring')表格,因此我将其附加到 jquery 列表中.add()

于 2013-02-17T16:42:30.910 回答
0

创建一个函数并将其用作处理程序:

$(document).on('change click blur', '.roomFac', eventFunc);
$("#park2").on("change click blur", eventFunc);
$("#lecturestyle2").on("change click blur", eventFunc);
$("#room_structure2").on("change click blur", eventFunc);
$("#slider2").on("change click blur", eventFunc);

function eventFunc()
{
    var park = $("#park2").val();
    var lecturestyle = $("#lecture_style2").val();
    var roomstructure = $("#room_structure2").val();
    var groupsize = $("#groupSize2").val();
    var facilities = "";
    $('select[name*=roomFac]').each(function () {
        facilities += $(this).val();
        facilities += ",";
    });
    var dataString = 'park=' + park + '&' + 'lecturestyle=' + lecturestyle + '&' +
        'roomstructure=' + roomstructure + '&' + 'groupsize=' + groupsize + '&' +
        'facilities=' + facilities;
    $.ajax({
        type: "POST",
        url: "process_timetableMon2.php",
        data: dataString,
        cache: false,
        success: function (html) {
            $('#mon').html(html);
        }
    });
}
于 2013-02-17T16:45:16.140 回答