2013-02-17 58 views
0

结合下面的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); 
      } 
     }); 
    }); 
+1

有回调被命名的功能,并给予VA把零件作为参数? – hd1 2013-02-17 16:40:42

回答

0

附加到jQuery的列表创建功能和使用作为处理者:

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

使用逗号:

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

既然你不能把$('document')$('somestring')形式,我就通过.add()