JQuery 代码对 Google Chrome 没有影响,但在 Firefox 中运行良好

发布于 2024-12-03 03:53:42 字数 2029 浏览 1 评论 0原文

我使用一段 JQuery 代码和一些 Mootools 来更改某些输入的类,但问题是它在 Google Chrome 中不起作用(没有效果),尽管它在 Firefox 中工作得很好。

编辑:我发现,如果我在页面上的给定元素上单击两次,该类就会被有效添加。

我的代码

<script type="text/javascript">
  window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
     // code inside works perfectly
});

$('votconj_no').addEvent('click', function() {
    // code inside works perfectly
});

$('nb_children').addEvent('click', function() {
  var selected = $('nb_children').getSelected();


  selected.each(function(element) {
  var val = element.get('value');
  for (var counter = 1; counter <= val; counter++) {
    $('jj_enfant' + counter).addClass("validate['required']");
    myCheck.register($('jj_enfant' + counter));
    $('mm_enfant' + counter).addClass("validate['required']");
    myCheck.register($('mm_enfant' + counter));
    $('aaaa_enfant' + counter).addClass("validate['required']");
    myCheck.register($('aaaa_enfant' + counter));
    $('last_name_enfant' + counter).addClass("validate['required','nodigit']");
    myCheck.register($('last_name_enfant' + counter));
    $('first_name_enfant' + counter).addClass("validate['required','nodigit']");
    myCheck.register($('first_name_enfant' + counter));
    // Here the good value is displayed
    alert(val);
  }

  for (var counter = parseInt(val)+1; counter <= 6; counter++) {
    $('jj_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('jj_enfant' + counter));
    $('mm_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('mm_enfant' + counter));
    $('aaaa_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('aaaa_enfant' + counter));
    $('last_name_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('last_name_enfant' + counter));
    $('first_name_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('first_name_enfant' + counter));
  }
  });
});

})
...
</script>

I use a piece of JQuery code with a bit of Mootools to change the class of some inputs but the thing is that it doesn't work (has no effect) in Google Chrome despite working perfectly in Firefox.

Edit : I've discovered that if I click two times on the given element on the page the class gets effectively added.

my code

<script type="text/javascript">
  window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
     // code inside works perfectly
});

$('votconj_no').addEvent('click', function() {
    // code inside works perfectly
});

$('nb_children').addEvent('click', function() {
  var selected = $('nb_children').getSelected();


  selected.each(function(element) {
  var val = element.get('value');
  for (var counter = 1; counter <= val; counter++) {
    $('jj_enfant' + counter).addClass("validate['required']");
    myCheck.register($('jj_enfant' + counter));
    $('mm_enfant' + counter).addClass("validate['required']");
    myCheck.register($('mm_enfant' + counter));
    $('aaaa_enfant' + counter).addClass("validate['required']");
    myCheck.register($('aaaa_enfant' + counter));
    $('last_name_enfant' + counter).addClass("validate['required','nodigit']");
    myCheck.register($('last_name_enfant' + counter));
    $('first_name_enfant' + counter).addClass("validate['required','nodigit']");
    myCheck.register($('first_name_enfant' + counter));
    // Here the good value is displayed
    alert(val);
  }

  for (var counter = parseInt(val)+1; counter <= 6; counter++) {
    $('jj_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('jj_enfant' + counter));
    $('mm_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('mm_enfant' + counter));
    $('aaaa_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('aaaa_enfant' + counter));
    $('last_name_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('last_name_enfant' + counter));
    $('first_name_enfant' + counter).removeAttribute('class');
    myCheck.dispose($('first_name_enfant' + counter));
  }
  });
});

})
...
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的鱼塘能养鲲 2024-12-10 03:53:42

为什么不使用 jQuery Ready() 来检测 Dom 何时准备就绪。
jQuery 旨在以跨浏览器的方式检测此事件,

替换

window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
    // code inside works perfectly
});

$(document).ready( function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
        // code inside works perfectly
    });
});

why don't you use jQuery ready() for detecting when then the Dom is ready.
jQuery is intended to detect this event in a cross browser way

replace

window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
    // code inside works perfectly
});

with

$(document).ready( function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
        // code inside works perfectly
    });
});
維他命╮ 2024-12-10 03:53:42

Firefox 6 或 Chrome 13 中都没有 addEvent 方法,至少在标准合规模式下没有。 (也许您想使用 addEventListener 方法?)

如果您使用 Quirks 模式,某些浏览器支持一些非标准方法,但如果您希望它跨浏览器工作,则不能使用这些方法。

请改用 jQuery 中的 ready 事件,该事件还为不支持该事件的浏览器(即 IE)模拟 domready 事件。

绑定事件处理程序的 jQuery 方法名为 bind,而不是 addEvent。您还可以使用click方法来绑定点击事件。

$(document).ready(function(){
  var myCheck = new FormCheck('formulaire');
  $('votconj').click(function() {
    // ...
  });
  // ...
});

请注意,HTML 中没有 votconj 元素,因此上面的选择器不匹配任何内容。

There is no addEvent method, neither in Firefox 6 or Chrome 13, at least not in Standards Compliance Mode. (Perhaps you wanted to use the addEventListener method?)

If you are using Quirks Mode some browsers are supporting some non-standard methods, but you can't use those methods if you want it to work cross browser.

Use the ready event in jQuery instead, which also emulates the domready event for browsers that doesn't support it (i.e. IE).

The jQuery method to bind event handlers is named bind, not addEvent. You can also use the click method to bind the click event.

$(document).ready(function(){
  var myCheck = new FormCheck('formulaire');
  $('votconj').click(function() {
    // ...
  });
  // ...
});

Note that there is no votconj element in HTML, so the selector above doesn't match anything.

春花秋月 2024-12-10 03:53:42

以下代码效果很好(我已将事件从单击更改为模糊,并且通过仅获取所选数组的最后一个元素,使代码变得更加简单和清晰)。

<script type="text/javascript">
  window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
      // code inside works
    });

    $('votconj_no').addEvent('click', function() {
     // code inside works
    });

    $('nb_children').addEvent('blur', function() {
       var selected = $('nb_children').getSelected();

       var element = selected.getLast();

       var val = element.get('value');
       for (var counter = 1; counter <= val; counter++) {
          $('jj_enfant' + counter).addClass("validate['required']");
          myCheck.register($('jj_enfant' + counter));
          $('mm_enfant' + counter).addClass("validate['required']");
          myCheck.register($('mm_enfant' + counter));
          $('aaaa_enfant' + counter).addClass("validate['required']");
          myCheck.register($('aaaa_enfant' + counter));
          $('last_name_enfant' + counter).addClass("validate['required','nodigit']");
          myCheck.register($('last_name_enfant' + counter));
          $('first_name_enfant' + counter).addClass("validate['required','nodigit']");
          myCheck.register($('first_name_enfant' + counter));
       }

       for (var counter = parseInt(val)+1; counter <= 6; counter++) {
          $('jj_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('jj_enfant' + counter));
          $('mm_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('mm_enfant' + counter));
          $('aaaa_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('aaaa_enfant' + counter));
          $('last_name_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('last_name_enfant' + counter));
          $('first_name_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('first_name_enfant' + counter));
       }

    });

})
...
</script>

The following code works great (I've changed the event from click to blur and I've made the code a bit simpler and cleaner by getting only the last element of the array selected).

<script type="text/javascript">
  window.addEvent('domready', function(){
    var myCheck = new FormCheck('formulaire');
    $('votconj').addEvent('click', function() {
      // code inside works
    });

    $('votconj_no').addEvent('click', function() {
     // code inside works
    });

    $('nb_children').addEvent('blur', function() {
       var selected = $('nb_children').getSelected();

       var element = selected.getLast();

       var val = element.get('value');
       for (var counter = 1; counter <= val; counter++) {
          $('jj_enfant' + counter).addClass("validate['required']");
          myCheck.register($('jj_enfant' + counter));
          $('mm_enfant' + counter).addClass("validate['required']");
          myCheck.register($('mm_enfant' + counter));
          $('aaaa_enfant' + counter).addClass("validate['required']");
          myCheck.register($('aaaa_enfant' + counter));
          $('last_name_enfant' + counter).addClass("validate['required','nodigit']");
          myCheck.register($('last_name_enfant' + counter));
          $('first_name_enfant' + counter).addClass("validate['required','nodigit']");
          myCheck.register($('first_name_enfant' + counter));
       }

       for (var counter = parseInt(val)+1; counter <= 6; counter++) {
          $('jj_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('jj_enfant' + counter));
          $('mm_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('mm_enfant' + counter));
          $('aaaa_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('aaaa_enfant' + counter));
          $('last_name_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('last_name_enfant' + counter));
          $('first_name_enfant' + counter).removeAttribute('class');
          myCheck.dispose($('first_name_enfant' + counter));
       }

    });

})
...
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文