简体   繁体   English

选中并取消选中复选框

[英]Check and uncheck a checkbox when another is clicked

I've got a page where I have 2 sets of checkboxes. 我有一个页面,其中有2套复选框。 When a checkbox on the bottom in Set 2 gets checked off it checks off its corresponding checkbox in the set 1 above it. 当Set 2底部的复选框被选中时,它会选中上方Set 1中的相应复选框。 Here's the thing, I have an "Add" button under the first set 1 of checkboxes that duplicates the set 2 of checkboxes. 事情就是这样,我在复选框的第一组1下有一个“添加”按钮,它与复选框的第2组重复。 However, this is only to add another line item to a specific department. 但是,这只是将另一个订单项添加到特定部门。

So a user can issue a workorder with multiple tasks for multiple departments attached to it. 因此,用户可以为附有多个部门的多个任务下达具有多个任务的工作单。

SEE EDIT FOR DETAILED EXPLANATION. 有关详细说明,请参见编辑。

The problem now is that the first lineup item check offs work but any other lineup item I add doesn't work when checking off. 现在的问题是,第一个选单项签出正常工作,但是我添加的任何其他选单项在签出时都不起作用。

$('.cad, .design, .shop, .cnc').on('change', function() {
    // Split into space-delimited array in case we want
    // to check off more than one
    var elemsToCheck = $(this).data('check').split(' ');
    var checked = $(this).attr('checked');
    $.each(elemsToCheck, function() {
        $('#' + this).attr('checked', !!checked);
    });
});

You can take a look at my jquery in this jsfiddle for code to see what I tried: http://jsfiddle.net/Bg5Uk/ 您可以在此jsfiddle中查看我的jquery,以查看我尝试的代码: http : //jsfiddle.net/Bg5Uk/

EDIT: Going to try to explain how this works again. 编辑:将尝试解释这又如何工作。

A tool (lets call it Tool X) is ready to be kicked off and manufactured so a manager has to submit a work order to his employees. 准备好启动一个工具(称为工具X)并进行制造,因此经理必须向其员工提交工单。

  • The manager will create a work order for Tool X and will be presented with something similar to the form in my fiddle. 经理将为Tool X创建一个工单,并向其显示类似于我的小提琴中的表格。

  • Now, the manager wants multiple "Tasks" (Lineups) completed for Tool X and each "Task" will be assigned to a separate department. 现在,经理希望为工具X完成多个“任务”(阵容),每个“任务”将分配到一个单独的部门。

  • The manager starts filling out Task 1 (Lineup #1) and once completed, he checks off C - CAD on the lineup. 经理开始填写任务1(阵容1),并在完成后检查阵容中的C-CAD。 This will check off CAD in the above set. 这将检查以上集合中的CAD。 So, the workorder will now notify the CAD department of the Workorder so they can start the work. 因此,工单现在将通知CAD工单,以便他们可以开始工作。

  • The manager isn't done, now he wants to fill out 2 more Tasks. 经理还没有完成,现在他想再填写2个任务。 He'll have to click the "ADD" button at the bottom of the form to add another TASK form (Line up #2). 他将必须单击表单底部的“添加”按钮来添加另一个任务表单(第2行)。 He fills it out and checks off M - CNC and S - SHOP which will check off CNC and SHOP in the first set of checkboxes above. 他将其填写并选中M-CNC和S-SHOP,这将在上面的第一组复选框中选中CNC和SHOP。

  • Now the workorder not only has CAD assigned to it, but it also has CNC and SHOP, which will also be notified, however, their work is related to Task 2 (Lineup #2). 现在,不仅为工作单分配了CAD,而且为工作单分配了CNC和SHOP,它们也将得到通知,但是,它们的工作与任务2(阵容2)有关。

  • Finally, the manager creates one last task (lineup #3). 最后,经理创建最后一个任务(阵容3)。 Task 3, he checks off D - DESIGN and S - SHOP. 任务3,他检查了D-DESIGN和S-SHOP。 Which will check off just Design in Set 1 of the checkboxes, don't forget, Shop has already been checked off so they will be notified of the Workorder already. 它将仅在复选框的集合1中选中“设计”,不要忘记,“商店”已被选中,因此他们将收到工作单通知。 They'll know that Task 3 is also for them to complete because I've already got that part working. 他们会知道任务3也是他们要完成的,因为我已经完成了那部分工作。

Now, when the manager submits the workorder, CAD, CNC, SHOP, and DESIGN will all get an email notification so they can read the work order and begin their work. 现在,当经理提交工作单时,CAD,CNC,SHOP和DESIGN都将收到电子邮件通知,以便他们阅读工作单并开始工作。

The problem: The check offs are only working on the first Task (1) (Lineup #1). 问题:结帐仅适用于第一个任务(1)(阵容1)。 Any other added task will not check off a corresponding department. 任何其他添加的任务都不会检查相应部门。 This is an issue because Task 2 and 3 may have Design and Shop checked off on the Task, but the department will not get an email because they weren't checked off on the first set of checkboxes. 这是一个问题,因为任务2和3可能已在“任务”上选中了“设计和购买”,但是部门未收到电子邮件,因为未在第一组复选框上选中它们。

You may be asking yourself, why doesn't the manager just double check the form and check off the departments that the notification and workorder should be sent to. 您可能会问自己,经理为什么不仔细检查表格并检查通知和工作单应发送到的部门。 I don't even know the answer, I just think it comes down to laziness and rushing through filling out this stuff. 我什至不知道答案,我只是认为这归因于懒惰并匆忙填写这些东西。

EDIT 2: 编辑2:

If I use something like below to check the values returning. 如果我使用下面的方法检查返回的值。 For the first Lineup checkboxes it returns the checkbox it should be checking off. 对于第一个Lineup复选框,它返回应该选中的复选框。 However, for Lineup #2 when I add a new lineup, nothing gets returned. 但是,对于阵容2,当我添加新的阵容时,什么也没有返回。 I don't get it... 我不明白...

$("input[name^='lineitem']").on('change', function() {
    var elemsToCheck = $(this).data('check').split(' ');
    var checked = $(this).prop('checked');
    alert(elemsToCheck); //returns cad, design, shop, cnc, etc.
    $.each(elemsToCheck, function() {
        $('#' + this).prop('checked', !!checked);
    });
}); 

if the code works for the first row, which according to you it does ("The check offs are only working on the first Task (1) (Lineup #1)."), this could most likely fix the problem you're facing: 如果代码对第一行有效(根据您的意愿)(“检查仅对第一项任务(1)(阵容#1)起作用”),则很可能可以解决您面临的问题:

just replace the given part of your code with this: 只需将代码的给定部分替换为:

$(document).on('change','.cad, .design, .shop, .cnc', function() {
    var elemsToCheck = $(this).data('check').split(' ');
    var checked = $(this).prop('checked');
    $.each(elemsToCheck, function() {
        $('#' + this).prop('checked', !!checked);
    });

note that I also changed the attr with prop 请注意,我也用prop更改了attr

UPDATE: 更新:

take a look at your code here: 在这里查看您的代码:

 $template = '<tr>\
            <td><input type="hidden" id="number" value="${num}" /><input style="width:20px" class="number" name="lineitem[${num}][num]" value="${num}" /></td>\
            <td>\
            <ul>\
            <li><input class="cad" type="checkbox" name="lineitem[${num}][affects][cad]" /><label><strong>C</strong> - CAD</label></li>\
            <li><input class="design" type="checkbox" name="lineitem[${num}][affects][design]" /><label><strong>D</strong> - DESIGN</label></li>\
            <li><input class="shop" type="checkbox" name="lineitem[${num}][affects][shop]" /><label><strong>S</strong> - SHOP</label></li>\
            <li><input class="cnc" type="checkbox" name="lineitem[${num}][affects][cnc]" /><label><strong>M</strong> - CNC</label></li>\
            </ul>\
            </td>\
            <td>\
            <ul>\
            <li><input type="checkbox" name="lineitem[${num}][engineering_changes]" /><label>Engineering</label></li>\
            <li><input type="checkbox" name="lineitem[${num}][correction_changes]" /><label>Correction</label></li>\
            </ul>\
            </td>\
            <td><textarea style="width:100%; height:100%" name="lineitem[${num}][desc]"></textarea></td>\
            <td><a class="remove" href="javascript:void(0)">Remove</a></td>\
            </tr>';

data-check is not defined for any of your elements here in your template, try defining it for the newly added items here and you're good to go. 尚未在模板中为您的任何元素定义data-check ,请尝试在此处为新添加的项目定义data-check ,您可以开始使用。

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

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