【问题标题】:Simplifying JavaScript code [closed]简化 JavaScript 代码 [关闭]
【发布时间】:2012-10-12 18:59:41
【问题描述】:

我想简化这段代码,并希望得到任何建议。

页面结构:

  • 有 10 个不同的部分。
  • 每个部分都有一个问题。
  • 每个问题都有三个答案。
  • 每个答案都有一个复选框。
  • 当用户选中复选框时,会显示针对该特定答案的反馈。
  • 当用户选中另一个复选框时,所有其他答案和复选框都会重置。

我已经创建了功能以使其在三个功能中工作。每个答案一个。为了使每个部分都可以使用,我需要创建 30 个函数。我确信有一个更简单的方法,我只是不知道从哪里开始。

我的代码

// Action 1
$('.choice input.choice-a:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-1 .mod3-6-1_choice_A .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-1 .mod3-6-1_choice_A .mod3-6-1_feedback").focus();
});
$('.choice input.choice-b:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-1 .mod3-6-1_choice_B .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-1 .mod3-6-1_choice_B .mod3-6-1_feedback").focus();

});
$('.choice input.choice-c:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-1 .mod3-6-1_choice_C .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-1 .mod3-6-1_choice_C .mod3-6-1_feedback").focus();
});
// Action 2
$('.choice input.choice-a:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-2 .mod3-6-1_choice_A .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-2 .mod3-6-1_choice_A .mod3-6-1_feedback").focus();
});
$('.choice input.choice-b:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-2 .mod3-6-1_choice_B .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-2 .mod3-6-1_choice_B .mod3-6-1_feedback").focus();

});
$('.choice input.choice-c:checkbox').on('change', function(){
    $('.choice input:checkbox').attr('checked', false);
    $(this).attr('checked', true);
    hide_choices();
    $("#action-2 .mod3-6-1_choice_C .mod3-6-1_feedback").removeClass("screen-reader");
    $("#action-2 .mod3-6-1_choice_C .mod3-6-1_feedback").focus();
});

Action 1 和 Action 2 的唯一区别是父 div 向用户显示反馈。

【问题讨论】:

  • 查看codereview.stackexchange.com。那会是一个更好的地方问这个问题。
  • 发布与此相关的 HTML,或者更好地制作一个 jsFiddle。

标签: javascript jquery input checkbox


【解决方案1】:

一个简单的改进是使用单选按钮而不是复选框,您可以删除这些行:

$('.choice input:checkbox').attr('checked', false);
$(this).attr('checked', true);

编辑。这是我将如何尝试的方法。输入元素需要 data-action="1" 和 data-choice="A" 属性。

$('.choice input').on('change', function(){
    var action = $(this).data('action');
    var choice = $(this).data('choice');

    $("#action-" + action).find(".mod3-6-1_choice_" + choice).find(".mod3-6-1_feedback").removeClass("screen-reader").focus();
});

【讨论】:

  • +1 这个答案比我的要好。我认为代码可以使用比我们看到的更多的清理。似乎 OP 使用了很多类来完成它们并不真正适用的事情。
  • +1 如果您希望一次只能选择一个项目,请不要使用复选框。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-02-18
  • 1970-01-01
  • 1970-01-01
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 2014-01-05
相关资源
最近更新 更多