javascript - 简化 JavaScript 代码

标签 javascript jquery input checkbox

<分区>

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

页面结构:

  • 有 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。

最佳答案

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

$('.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();
});

关于javascript - 简化 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12865438/

相关文章:

c - 为什么我的 scanf 不能正常工作,而当我移动它时,它工作正常?

javascript - 如何将 Redux 与超大数据集和 IndexedDB 集成

c# - 可以从 javascript/jquery 获取返回 url 吗?

javascript - 在日期输入中阻止一组日期,这可能吗?

javascript - 使用 AngularJS 函数设置 'value' 输入

jquery:尝试获取 div 高度时的值错误

javascript - jQuery 有 "exists"函数吗?

javascript - 对象属性未定义 (javascript)

php - 更改下拉菜单执行 jquery 函数和 sql 查询

javascript - jQuery Sortable 不能拖放