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