开发者

JQuery code having no effect on Google Chrome but working perfectly in Firefox

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>


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
    });
});


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜