Uncaught ReferenceError: check is not defined

Uncaught ReferenceError: check is not defined

请帮我解决这个问题,jquery 在 w3schools 中运行良好,但在我的 rails 2 应用程序中出现错误,如 check not defined 这是我的 html 代码:

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"/>

和Jquery如下

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

上面的代码运行良好,但是当我将它插入我的 rails 应用程序时出现错误,我不知道哪里出错了。 任何帮助都是有价值的

script elements 可以具有 src 属性 内容,但不能同时具有两者。如果两者都有,则忽略内容(内容被视为 "script documentation," 而不是代码)。

为您的 jQuery 脚本使用另一个脚本块

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

在您使用 jQuery 时使用它绑定事件处理程序。

1.Your 脚本未正确加载。
2.You 加载的脚本不在文档范围内。

注意:您必须使用单独的脚本标签来加载外部 JS 文件和编写 JS 代码。

像这样尝试

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

in rails 2 JQUERY 不支持所以我将脚本更改为 Javascript 如下

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

感谢您的回答。上面的脚本对我有用

出现此错误是因为您将函数定义到 DOM 就绪事件的范围内...... 而不是尝试在 Function 范围之外定义您的函数。