@fuckingawesomenigga

Как укоротить код?

возможно ли сделать из этого функцию?

spoiler
$(".male #footwear").click(function(event) {
    $.ajax({
            url: 'output.php',
            data: { gender: 'male', category: 'footwear'},
        })
        .done(function(data) {
        	$('section').removeClass('show');
        	$(".items-list").addClass('show');
            $(".items-list .title").text('Male footwear');
            $(".items-list .items").text(data);
        })
        .fail(function() {
            console.log("error");
        });
});

$(".female #footwear").click(function(event) {
    $.ajax({
            url: 'output.php',
            data: { gender: 'female', category: 'footwear'},
        })
        .done(function(data) {
            $('section').removeClass('show');
            $(".items-list").addClass('show');
            $(".items-list .title").text('Female footwear');
            $(".items-list .items").text(data);
        })
        .fail(function() {
            console.log("error");
        });
});

$(".female #clothes").click(function(event) {
    $.ajax({
            url: 'output.php',
            data: { gender: 'female', category: 'clothes'},
        })
        .done(function(data) {
            $('section').removeClass('show');
            $(".items-list").addClass('show');
            $(".items-list .title").text('Female clothes');
            $(".items-list .items").text(data);
        })
        .fail(function() {
            console.log("error");
        });
});
  • Вопрос задан
  • 165 просмотров
Решения вопроса 1
ivan_zhuck
@ivan_zhuck
Если по-быстрому и не очень аккуратно, то можно так:

var selectCategory = function(gender){

  $("#"+gender).click(function(event) {
    $('section').removeClass('show');
    $("."+gender).addClass('show');
  });

}

var loadCategory = function(options){

  $("."+options.gender+" #"+slug).click(function(event) {
    $.ajax({
            url: 'output.php',
            data: { gender: gender, category: options.slug},
        })
        .done(function(data) {
          $('section').removeClass('show');
          $(".items-list").addClass('show');
            $(".items-list .title").text(options.title);
            $(".items-list .items").text(data);
        })
        .fail(function() {
            console.log("error");
        });
  });

}

selectCategory('male');
selectCategory('female');

loadCategory({slug: 'footwear', gender: 'male', title: 'Male footwear'});
loadCategory({slug: 'sneakers', gender: 'male', title: 'Male sneakers'});
loadCategory({slug: 'clothes', gender: 'male', title: 'Male clothes'});
loadCategory({slug: 'footwear', gender: female', title: 'Male footwear'});
loadCategory({slug: 'sneakers', gender: 'female', title: 'Male sneakers'});
loadCategory({slug: 'clothes', gender: 'female', title: 'Male clothes'});
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
18 апр. 2024, в 11:47
30000 руб./за проект
18 апр. 2024, в 11:46
5000 руб./за проект
18 апр. 2024, в 11:43
10000 руб./за проект