简体   繁体   English

removeClass如果单击了addClass?

[英]removeClass if addClass clicked?

If a new ranking is clicked how can we automatically declick the previous click? 如果点击了新排名,我们如何自动取消上一次点击?

<%= f.hidden_field(:ranking, id: 'ranking') %>
<%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker') %>
<%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker') %>
<%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker') %>
<%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker') %>

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    if ($('#ranking').hasClass('clicked')){ #Conditional doesn't work. If user clicks on second image the first image will stay clicked
      $('.clicked').removeClass('clicked')
    }else{
      $(this).addClass('clicked');
    }
  });
</script>

I tried to incorporate the answers from here: removeClass() if it exists , but they didn't work for me. 我试图从这里合并答案: removeClass()如果存在 ,但是它们对我不起作用。

Place your code inside document.ready 将您的代码放入document.ready

<script>
    $(document).ready(function(){


         //Place your javascript /jquery code here

    })
</script>

i think Aju John is right. 我认为Aju John是对的。 But maybe a little additional tip. 但也许还有一点额外的提示。

you use $(this) a couple of lines after the click event. 您可以在$ click事件之后使用$(this)几行。 In my experience it's better to declare a variable and assign $(this) to that variable. 以我的经验,最好声明一个变量并将$(this)分配给该变量。 If you don't, you risk changing the $(this) value before you use it. 如果不这样做,则可能会在使用$(this)值之前冒险更改它。 what i do: 我所做的:

$('.image-clicker').click(function(){
{
    var r = $(this);
    $('.image-clicker').removeClass('clicked');
    r.addClass('clicked')
}

And why do you check your hidden fild for the 'clicked' class ? 以及为什么要检查隐藏的fild是否为“ clicked”类? Shouldn't you be checking the image-clickers ? 您不应该检查图像点击器吗?

The 'click' event is assigned to your 'image-clicker' elements, but the 'clicked' class is added to the hidden field. “单击”事件已分配给“图像单击器”元素,但“已单击”类已添加到隐藏字段。 The element references are mixed up, and the code needs to check to see if the image has already been clicked. 元素引用混合在一起,并且代码需要检查以查看图像是否已被单击。

This might be closer to what you're trying to do: 这可能更接近您要执行的操作:

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    var already_clicked = $(this).hasClass('clicked');
    $('.clicked').removeClass('clicked')
    if (!already_clicked) {
      $(this).addClass('clicked');
    }
  });
</script>

The already_clicked check determines if the image is already clicked, so that it can tell whether to keep click it after clearing the existing 'clicked' classes from the other images. already_clicked检查确定图像已被点击,它可以告诉是否保留单击从其他图像清理现有的“点击”类后。

Note that this will allow all images to become 'unclicked'. 请注意,这将使所有图像变为“未单击”状态。 If you want this to behave like radio buttons, you can leave out the check for already_clicked and always add the 'clicked' class to the element that was clicked. 如果您希望它的行为像单选按钮一样,则already_clicked已经为already_clicked的检查,并始终将“ clicked”类添加到被单击的元素中。 This will do that: 这样可以做到:

<script>
  $('.image-clicker').click(function() {
    $('#ranking').val($(this).data('ranking'));
    $('.clicked').removeClass('clicked')
    $(this).addClass('clicked');
  });
</script>

In this case, you'll likely also want to start your radio buttons in a known state. 在这种情况下,您可能还希望以已知状态启动单选按钮。 In the original markup (ERB), you would want to add the 'clicked' class to one of the image_tag elements. 在原始标记(ERB)中,您想将'clicked'类添加到image_tag元素之一。 You simply have to add the 'clicked' class to one of the images at creation time, like so: 您只需在创建时将“ clicked”类添加到其中一张图像即可,如下所示:

<%= f.hidden_field(:ranking, id: 'ranking') %>
<%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker clicked']) %>
<%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker') %>
<%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker') %>
<%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker') %>

You can also do this dynamically, based on the value of a variable or field from your model. 您还可以根据模型中变量或字段的值动态地执行此操作。 Assuming that a variable named ranking contains this value, consider this initialization code: 假设一个名为变量ranking包含此值,考虑这个初始化代码:

<%= f.hidden_field(:ranking, id: 'ranking') %>
<%= image_tag('4.png', data: { ranking: 4 }, class: 'image-clicker' + (ranking == '4' ? ' clicked' : '')) %>
<%= image_tag('3.png', data: { ranking: 3 }, class: 'image-clicker' + (ranking == '3' ? ' clicked' : '')) %>
<%= image_tag('2.png', data: { ranking: 2 }, class: 'image-clicker' + (ranking == '2' ? ' clicked' : '')) %>
<%= image_tag('1.png', data: { ranking: 1 }, class: 'image-clicker' + (ranking == '1' ? ' clicked' : '')) %>

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

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