繁体   English   中英

jQuery的复选框监听器循环

[英]jquery checkbox listener for loop

https://github.com/Campos696/Attendance/commit/28177ff5cf285e9616faddae74fa6f0288a8667a

我有这个javascript文件:

https://github.com/Campos696/Attendance/blob/master/js/app.js

并试图使它由bodyView创建的复选框具有单击侦听器,但是现在我一次只能为一个复选框创建一个侦听器,有什么方法可以改善这一点?

以及我是否选中它都做相同的事情(减少daysMissed)。

这是代码的相关部分:

var bodyView = {

  init: function() {
    this.render();
  },

  render: function() {
    var student, i, x;

    var schoolDays = octopus.getSchoolDays();
    var students = octopus.getStudents();


    for(i = 0;i < students.length; i++) {
      octopus.setCurrentStudent(students[i]);
      var daysMissed = octopus.getDaysMissed();
      $('#students').append('<tr class="'+i+'"><td class="name-col">' + students[i].name + '</td></tr>');
      for(x = 1;x < schoolDays; x++) {
        $('.'+i).append('<td id="check'+i+'" class="attend-col"><input type="checkbox"></td>');
      };
      $('.'+i).append('<td class="missed-col">'+ daysMissed + '</td>');
    };
    $(function(){
      $('#check0').click(function() {
        octopus.setCurrentStudent(students[0]);
        if($(this).is(':checked')){
          octopus.incrementDaysMissed();
        } else if(!$(this).is(':checked')){
          octopus.decreaseDaysMissed();
        }
      })
    })
  }
}

功能编辑

$(function(){
         $('[id^=check] :checkbox').on('change', function(e) {                
            var daysMissed = $(this).closest('tr').find('td:last');

            if (this.checked) {
              octopus.decreaseDaysMissed();
              daysMissed.html(octopus.getDaysMissed());
            } else {
              octopus.incrementDaysMissed();
              daysMissed.html(octopus.getDaysMissed());
            }
        })
    })

ID必须是唯一的。 这意味着您需要更改以下行:

$('.'+i).append('<td id="check'+i+'" class="attend-col"><input type="checkbox"></td>');

与:

$('.'+i).append('<td id="check'+ i + x +'" class="attend-col"><input type="checkbox"></td>');
                               ^^^^^^^^

这样,每个td都有一个ID,例如:check01 ..... check46 ...

第二个问题:使用更改事件更改点击事件,并将其附加到:

$('[id^=check] :checkbox').on('change', function(e) {

选择每个TD有一个id开始检查 ,并为每个TD得到孩子复选框。

 var model = { currentStudent: null, schoolDays: 12, students: [ { name: "Slappy the Frog", daysMissed: 12 }, { name: "Lilly the Lizard", daysMissed: 12 }, { name: "Paulrus the Walrus", daysMissed: 12 }, { name: "Gregory the Goat", daysMissed: 12 }, { name: "Adam the Anaconda", daysMissed: 12 } ] }; // Octopus var octopus = { init: function () { model.currentStudent = model.students[0]; headerView.init(); bodyView.init(); }, getStudents: function () { return model.students; }, setCurrentStudent: function (student) { model.currentStudent = student; }, getSchoolDays: function () { return model.schoolDays + 1; }, getDaysMissed: function () { return model.currentStudent.daysMissed; }, incrementDaysMissed: function () { model.currentStudent.daysMissed++; }, decreaseDaysMissed: function () { model.currentStudent.daysMissed--; } }; // View var headerView = { init: function () { this.render(); }, render: function () { var i; var schoolDays = octopus.getSchoolDays(); $('#header').append('<th class="name-col">Student Name</th>'); for (i = 1; i < schoolDays; i++) { $('#header').append('<th>' + i + '</th>'); } ; $('#header').append('<th class="missed-col">Days Missed</th>'); } }; var bodyView = { init: function () { this.render(); }, render: function () { var student, i, x; var schoolDays = octopus.getSchoolDays(); var students = octopus.getStudents(); $('#students').empty(); for (i = 0; i < students.length; i++) { octopus.setCurrentStudent(students[i]); var daysMissed = octopus.getDaysMissed(); $('#students').append('<tr class="' + i + '"><td class="name-col">' + students[i].name + '</td></tr>'); for (x = 1; x < schoolDays; x++) { $('.' + i).append('<td id="check' + i + x + '" class="attend-col"><input type="checkbox"></td>'); } $('.' + i).append('<td class="missed-col">' + daysMissed + '</td>'); } $(function(){ $('[id^=check] :checkbox').on('change', function(e) { var colId = $(this).closest('td').index(); var rowId = $(this).closest('tr').index(); var studentName = $(this).closest('tr').find('td:first').text(); console.log('Student: <' + studentName + '> on day: ' + colId + ' changed to: ' + this.checked + ' Col: ' + colId + ' Row: ' + rowId); }) }) } } $(function () { octopus.init(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" href="https://rawgit.com/Campos696/Attendance/master/css/style.css"> <h1>Udacity Attendance</h1> <table> <thead> <tr id="header"></tr> </thead> <tbody id="students"></tbody> </table> 

使用input:checkbox选择器来获取所有复选框:

$(function(){
  $("input:checkbox").click(function() {
    octopus.setCurrentStudent(students[0]);
    if($(this).prop("checked",true)){
      octopus.incrementDaysMissed();
    } else {
      octopus.decreaseDaysMissed();
    }
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM