0
我目前正在编写一些客户端JavaScript的一个Intranet网站,我正在开发,并开始感觉我是没有正确地做它(虽然它的工作原理)。Javascript监听器,处理程序,ajax
我做了大量的异步帖子,页面开始填满一堆(代码片段)lister/handler/xmlhttp块,它们基本上都做同样的事情。这是做事的正常方式还是有更优雅的方法?
//Select Location
$(document).on('change', '#location-select', function(e){
$.post('/Admin/SubLocations', jQuery.param({ location: $('#' + this.id).val() }),
function(response){
var json = JSON.parse(response);
if(json.error) {
$('#error').html(json.response);
} else {
$('#sublocation-list').html(json.response);
}
});
return false;
});
// New Location
$('#location-list').on('submit', '#newLocation', function(e){
$.post('/Admin/AddLocation', $(this).serialize(),
function(response){
var json = JSON.parse(response);
if(json.error) {
$('#error').html(json.response);
} else {
$('#location-list').html(json.response);
$('#sublocation-list').html('');
}
});
return false;
});
你也许可以利用'if..else'或有条件的经营者使用相同的事件处理程序,这两个事件。请参阅http://codereview.stackexchange.com/ – guest271314