我爲我的項目編寫了一個腳本,它允許用戶使用JS,PHP和MySql管理庫存。用戶可以選擇一個庫存位置並通過點擊按鈕來更新產品的數量。此過程使用AJAX調用來實時更新前端和後端。避免JavaScript中的多餘代碼(JQuery)
我的代碼有效,但是,由於代碼冗餘的數量,我對執行不滿意。我有兩個添加和刪除AJAX功能,以更新顯示在網站上的第一個庫存位置。但爲了讓用戶在選擇新位置後繼續執行此操作,我必須重用嵌套在選定位置Ajax代碼中的這兩個函數的代碼。我的JS技能並不強,我想知道JQuery是否允許更好的代碼內聚。在PHP中,我將創建一個類並執行此任務以避免代碼重用。我可以在JQuery中實現類似的功能嗎?
// This script is used update and display changes to inventory displayed on cp.php
$(document).ready(function() {
var this_item;
var itemid;
var count;
// Store the first table from the MySQL Database and display it to the page
var location = $('.location_select').attr('title');
$('.default_location').empty();
$('.default_location').append(location);
// Add to the inventory count of the selected product
$('.glyphicon-plus').click(function() {
event.preventDefault();
// Store the selected product name and update its display count on the page
this_item = $(this).attr('title');
itemid = this_item.substr(0, this_item.length - 1);
count = parseInt($('#'+itemid).text());
count++;
// Send the inventory count to modifyproduct.php and update new count to MySQL
$.ajax ({
url: 'modifyproduct.php',
type: 'GET',
data: {
location: location,
itemid: itemid,
count: count,
},
success: function(serverResponse) {
$('#'+itemid).hide();
$('#'+itemid).html(count).delay(170);
$('#'+itemid).fadeIn();
}
});
});
// Remove from the select inventory count
$('.glyphicon-minus').click(function() {
event.preventDefault();
// Store the selected product name and update its display count on the page
this_item = $(this).attr('title');
itemid = this_item.substr(0, this_item.length - 1);
count = parseInt($('#'+itemid).text());
count--;
// Send the inventory count to modifyproduct.php and update new count to MySQL
$.ajax ({
url: 'modifyproduct.php',
type: 'GET',
data: {
location: location,
itemid: itemid,
count: count,
},
success: function(serverResponse) {
$('#'+itemid).hide();
$('#'+itemid).html(count).delay(170);
$('#'+itemid).fadeIn();
}
});
});
// Select inventory location to be displayed from .location_select menu
$('.location_select').click(function(){
location = $(this).attr('title');
$('.default_location').empty();
$('.default_location').append(location);
$.ajax ({
url: 'display.php',
type: 'GET',
data: {
location: location,
},
success: function(serverResponse) {
$('#display_inventory').fadeOut('slow');
$('#display_inventory').empty();
$('#display_inventory').hide();
$('#display_inventory').append(serverResponse);
$('#display_inventory').fadeIn('slow');
$('.glyphicon-plus').click(function() {
event.preventDefault();
this_item = $(this).attr('title');
itemid = this_item.substr(0, this_item.length - 1);
count = parseInt($('#'+itemid).text());
count++;
$.ajax ({
url: 'modifyproduct.php',
type: 'GET',
data: {
location: location,
itemid: itemid,
count: count,
},
success: function(serverResponse) {
$('#'+itemid).hide();
$('#'+itemid).html(count).delay(170);
$('#'+itemid).fadeIn();
}
});
});
$('.glyphicon-minus').click(function() {
event.preventDefault();
this_item = $(this).attr('title');
itemid = this_item.substr(0, this_item.length - 1);
count = parseInt($('#'+itemid).text());
count--;
$.ajax ({
url: 'modifyproduct.php',
type: 'GET',
data: {
location: location,
itemid: itemid,
count: count,
},
success: function(serverResponse) {
$('#'+itemid).hide();
$('#'+itemid).html(count).delay(170);
$('#'+itemid).fadeIn();
}
});
});
}
})
});
});
ajax電話的性質只是一種冗長而羅嗦。只要你每次都做不同的Ajax調用,這就是做這件事的方法。您可以將'get'調用分配給'modifyproduct.php'到一些函數中,然後調用它以便不必重複相同的確切代碼。 – Wold
*「在PHP中,我將創建一個類並執行此任務以避免代碼重用。」* 只需對JS執行相同的操作,即可將邏輯與幾個函數分開。 – noppa
您可能會對[codereview.se]有更好的運氣 – mezmi