简体   繁体   English

jQuery:检查/取消选中DOM元素

[英]jQuery: Checking / Unchecking DOM Elements

I've got the following script that gets executed via AJAX in Rails: 我有以下脚本通过Rails中的AJAX执行:

It appears that only the first specified option (unchecking) works, the part inside the else statements does not work! 似乎只有第一个指定的选项(取消选中)有效,else语句内的部分无效!

<% if params[:type] == "switch" %>
    var obj = $('.<%= params[:name] %> a:not(.label) #myonoffswitch:checkbox');
    <% if params[params[:name]] %>
        obj.prop('checked', false);
    <% else %>
        obj.prop('checked', true);
    <% end %>
<% elsif params[:type] == "checkbox" %>
    <% if params[params[:name]] %>
        $('.<%= params[:name] %> a:not(.label)').remove();
        $('.<%= params[:name] %>').prepend("<a data-remote='true' href='/update?name=<%= params[:name] %>&amp;<%= params[:name] %>=true&amp;type=checkbox'><i class='icon ion-android-checkbox-outline-blank' tabindex='0'></i></a>");
        $('.<%= params[:name] %> a:not(.label) i').focus();
    <% else %>
        $('.<%= params[:name] %> a:not(.label)').remove();
        $('.<%= params[:name] %>').prepend("<a data-remote='true' href='/update?name=<%= params[:name] %>&amp;<%= params[:name] %>=false&amp;type=checkbox'><i class='icon ion-android-checkbox' tabindex='0'></i></a>");
        $('.<%= params[:name] %> a:not(.label) i').focus();
    <% end %>
<% end %>

DOM DOM

      <div class="name flex">
        <a data-remote="true" href="/update?name=name&amp;name=false&amp;type=switch"><div class="onoffswitch">
          <input checked="" class="onoffswitch-checkbox" id="myonoffswitch" name="onoffswitch" type="checkbox">
          <label class="onoffswitch-label" for="myonoffswitch"></label>
        </div>
        </a>
        <div class="label">
          <a class="abc label" data-remote="true" href="/update?name=name&amp;name=false&amp;type=switch">ABC
          </a>
          <p class="abc labeltext">Lorem ipsum.</p>
        </div>
      </div>

      <div class="name1 flex"><a data-remote="true" href="/update?name=name1&amp;name1=true&amp;type=checkbox">
        <i class="icon ion-android-checkbox-outline-blank" tabindex="0"></i></a>

        <div class="label">
          <a class="abc label" data-remote="true" href="/update?name=name1&amp;name1=false&amp;type=checkbox">ABC
          </a>
          <p class="abc labeltext">Lorem ipsum.</p>
        </div>
      </div>

However, I noticed that only if a checkbox or a switch is checked it gets unchecked, and if it is unchecked it will not change it's appearance (the DOM). 但是,我注意到,只有选中复选框或开关,它才会被选中;如果未被选中,它的外观(DOM)也不会改变。

What am I doing wrong? 我究竟做错了什么?

You need to remove :checked selector . 您需要删除:checked选择器 As of now you are only getting checked checkboxes 截至目前,您只得到checked复选框

Use 采用

var obj = $('.<%= params[:name] %> a:not(.label) #myonoffswitch:checkbox');

instead of 代替

var obj = $('.<%= params[:name] %> a:not(.label) #myonoffswitch:checkbox:checked');

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

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