4

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

Hemanth M C
  • 436
  • 1
  • 4
  • 18

4 Answers4

3

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

Hemanth M C
  • 436
  • 1
  • 4
  • 18
2

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.

Satpal
  • 132,252
  • 13
  • 159
  • 168
0

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>
J Santosh
  • 3,808
  • 2
  • 22
  • 43
0

This error is coming because you are defining your function to the scope of DOM Ready Event..... Instead of that try to define your function outside the scope of Function.

srbh
  • 1
  • 2