简体   繁体   中英

Uncaught ReferenceError: check is not defined

Please help me out with this, jquery is working well in w3schools but in my rails 2 app its getting error like check not defined Here is my html code:

Checkbox: <input class="category_select" title="3" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="6"/>

<input class="category_select" title="9" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="7"/>

<input class="category_select" title="6" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="8"/>

and the Jquery as follows

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js">
  function check(val,that) {
    var total=parseInt(0,10);
    $('.category_select').each(function(){
      if($(this).is(':checked')==true)
        total=total+parseInt($(this).attr('title'),10);
    });
    if (total > val){
      $(that).removeAttr('checked');
      alert('Total is greater than val !');

    }else{
      alert('Total is less than or equal to val !');
    }
  }
</script>

The above code works well , but when i insert it into my rails app its getting error , I dont know where I am going wrong. Any help is valuable

in rails 2 JQUERY is not supporting so i changed my script to Javascript as follows

<script type='text/javascript'>
  function check_total(e,t){ 
value=parseInt(e);
var a=parseInt(0);
$$("input.category_select").each(function(e){1==e.checked&&(a+=parseInt(e.title))}),a>value?(alert("Total is greater than Total Subcategory Hours !"),t.checked=!1):a==value&&alert("Total is equal to Total Subcategory Hours !")}
</script>

Thanks for your answers. the above script worked for me

script elements can have a src attribute or content, but not both. If they have both, the content is ignored (the content is considered "script documentation," not code).

Use another script block for your jQuery script

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.js">
</script>
<script type="text/javascript" >
  function check(val, that) {
      var total = parseInt(0, 10);
      $('.category_select').each(function() {
          if ($(this).is(':checked') == true)
              total = total + parseInt($(this).attr('title'), 10);
      });
      if (total > val) {
          $(that).removeAttr('checked');
          alert('Total is greater than val !');

      } else {
          alert('Total is less than or equal to val !');
      }
  }

  $(function() {
      $(document).on('change', '.category_select', function() {
          check(11, this);
      });
  });
</script>

As you are using jQuery bind event handlers using it.

1.Your script isn't loading properly.
2.You loaded script is not present in document scope.

Note : You have to use separate scripts tags to load external JS files and to write JS code.

try like this

 <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type='text/javascript'> function check(val, that) { var total = parseInt(0, 10); $('.category_select').each(function() { if ($(this).is(':checked') == true) total = total + parseInt($(this).attr('title'), 10); }); if (total > val) { $(that).removeAttr('checked'); alert('Total is greater than val !'); } else { alert('Total is less than or equal to val !'); } } </script> </head> <body> Checkbox: <input class="category_select" title="3" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="6" /> <input class="category_select" title="9" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="7" /> <input class="category_select" title="6" name="subject_ids[]" onchange="check(11,this)" type="checkbox" value="8" /> </body> </html> 

此错误即将发生,因为您正在将您的函数定义到DOM Ready事件的范围.....而不是尝试在Function范围之外定义您的函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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