简体   繁体   English

如何使用jQuery动态添加链接到复选框或输入标签中的值

[英]How to dynamically add values linked to a checkbox or in the input tag using jQuery

I have been at this for quite some time. 我在这里已经有一段时间了。 I am trying to create a form the does dynamic calculations using jQuery. 我正在尝试创建一个使用jQuery进行动态计算的表单。

I want to check the prop('checked') for the checkboxes in the input tags. 我想检查prop('checked')中输入标签中的复选框。 I don't think this is a problem, but for the form I am working on they have the checkboxes positioned off the screen since they are swapping out active and inactive images. 我不认为这是个问题,但是对于我正在处理的表单,由于它们交换出活动和不活动的图像,因此它们的复选框位于屏幕之外。 I should still be able to to check the prop('checked') in that input tag even though the checkbox is off screen, correct? 即使复选框不在屏幕上,我仍然应该能够在该输入标签中检查prop('checked') ,对吗? If so, this does not work for some reason. 如果是这样,则由于某种原因这是行不通的。

So I have been working on a work around. 所以我一直在努力工作。 I created an .on('click', function() {}); 我创建了一个.on('click', function() {}); that grabs the sibling element which would be the input tag and it's value. 它捕获了将成为输入标签及其值的同级元素。 This works. 这可行。 kind of. 的种类。 The value doesn't show in the #total-field until after the checkbox is unclicked. 直到取消选中复选框后,该值才会显示在#total字段中。 My thinking is because the click function is firing at the same time as when I am grabbing the var giantSub = $('#giant-sub-label'); 我的想法是因为单击功能与我抓取var giantSub = $('#giant-sub-label');的同时触发var giantSub = $('#giant-sub-label'); so the if(giantSub.hasClass('checked'); doesn't see the class checked until I uncheck the box which in turn puts my value in the #total-field afterwards. Is this correct? 所以if(giantSub.hasClass('checked');直到我取消选中该框后才看到该类已选中,这随后又将我的值放在#total-field 。这对吗?

If so, is there a way to check for the class checked after the checkbox is clicked before it is stored in the variable? 如果是这样,是否有一种方法可以在单击复选框后再将其存储在变量中,以检查是否选中了该类? So that the variable has the .checked I am looking for the pass my if condition which then would add the values in the #total field? 这样该变量具有.checked我正在寻找是否通过if条件,然后将条件添加到#total字段中? Or is there an easier way to do this? 还是有更简单的方法来做到这一点?

Here's my anonymous function: 这是我的匿名函数:

$(function () {
    $('#giant-sub-label').on('click', function () {
        var giantSub = $('#giant-sub-label');
        var total = 0;
        if (giantSub.hasClass('checked')) {
            total += parseInt($(this).next().val());
        }
        $('#total-field').val('$' + total);
    });
});

HTML HTML

<div class="checkbox-format">
  <label for="three-feet-field" id="giant-sub-label">3-feet</label>
  <input type="checkbox" name="GIANT_THREE_FEET" id="three-feet-field" class="order-options" value="42" />
</div>

The behaviour which you are getting is because of Synthetic Click Activation . 您得到的行为是由于“ 合成点击激活”
When a label is associated with some control(checkbox in this case), clicking the label will fire click event and since there is a controll associated, user agent will thereafter run synthetic click activation on the checkbox. 标签与某个控件关联时(在此情况下为复选框),单击标签将触发点击事件,并且由于存在控件关联,因此用户代理将在此复选框上运行综合点击激活。
Therefore you are getting the pre-click state of checkbox in your label click handler. 因此,您将在标签单击处理程序中获得复选框的预单击状态。
However you can get the post-click checkbox state by attaching handler to checkbox instead of label. 但是,可以通过将处理程序附加到复选框而不是标签来获得单击后复选框的状态。 These events will be fired indirectly with label click (even if your checkbox is hidden) after modifying the checkbox state. 修改复选框状态后,这些事件将通过单击标签间接触发(即使您的复选框处于隐藏状态)。

 $(checkBoxSelector).on('click or change', function () {...do something

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

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