在jQuery中加载时应用选中的事件 [英] Apply the checked event when loading in jquery

查看:53
本文介绍了在jQuery中加载时应用选中的事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 http://jsfiddle.net/daveSalomon/bcbxftr6/1/默认显示选中的事件:

I made a small change to the code at http://jsfiddle.net/daveSalomon/bcbxftr6/1/ to display the checked event by default:

HTML代码:

<input type="checkbox" class="notice" checked /><label>Featured</label>
<ol>
  <li>Coffee</li>
  <li>Juice</li>
  <li class="top">Tea</li>
  <li class="top">Milk</li>
  <li>Apple</li>
</ol>

jQuery:

var oldHtml = $('ol').html();
var reversed = false;

  $('.notice').change(function () {
    if ($(this).is(':checked')) {
    $.each($('li.top').get().reverse(), function(index, value) {
      $(value).insertBefore($('li')[0]);
    });
    reversed = true;
    $('li.top').show();
    return;
  }

  $('ol').html(oldHtml);
  reversed = false;
  $('li.top').hide();
});

(演示: http://jsfiddle.net/bcbxftr6/80/)

该代码的问题是,加载时未应用选中的事件.当我单击两次或更多次时,它会按预期工作.

The problem with the code is that the checked event is not applied when loading. When I click two or more times, it works as expected.

如何进行修改以加载应用了选中事件的页面?

How can it be modified to load the page with the checked event applied?

如果有任何不清楚的地方,请告诉我.

If there is anything unclear, please let me know.

谢谢.

推荐答案

未经测试,但可能正在加载脚本 $( document ).ready() 函数将发挥神奇作用:D

Not tested but may be loading the script whiting the $( document ).ready() function will do the magic :D

这篇关于在jQuery中加载时应用选中的事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆