结合类似的jQuery语句

时间:2013-02-17 16:38:01

标签: jquery

结合以下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);
            }
        });
    });

2 个答案:

答案 0 :(得分:0)

使用逗号:

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

由于您无法将$('document')放在$('somestring')表单中,我会通过.add()

将其附加到jquery列表中

答案 1 :(得分: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);
        }
    });
}
相关问题