Jquery 未捕获引用错误:未定义检查
请帮我解决这个问题,jquery在W3中运行良好,但在我的rails 2应用程序中,它的获取错误(如检查)未定义 以下是我的html代码:Jquery 未捕获引用错误:未定义检查,jquery,html,ruby-on-rails,ruby-1.8.7,Jquery,Html,Ruby On Rails,Ruby 1.8.7,请帮我解决这个问题,jquery在W3中运行良好,但在我的rails 2应用程序中,它的获取错误(如检查)未定义 以下是我的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" nam
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>
<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>
功能检查(val,即){
var-total=parseInt(0,10);
$('.category_select')。每个(函数(){
如果($(this).is(':checked')==true)
total=total+parseInt($(this).attr('title'),10);
});
如果(总计>val){
$(that.removeAttr('checked');
警报('总计大于val!');
}否则{
警报('Total小于或等于val!');
}
}
上面的代码运行得很好,但是当我将它插入rails应用程序时,它出现了getting错误,我不知道哪里出了问题。
任何帮助都是有价值的可以具有src
属性或内容,但不能两者兼有。如果两者都有,则忽略内容(内容被视为“脚本文档”,而不是代码)
为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>
功能检查(val,即){
var-total=parseInt(0,10);
$('.category_select')。每个(函数(){
如果($(this).is(':checked')==true)
total=total+parseInt($(this).attr('title'),10);
});
如果(总计>val){
$(that.removeAttr('checked');
警报('总计大于val!');
}否则{
警报('Total小于或等于val!');
}
}
$(函数(){
$(文档).on('change','category_select',function(){
检查(11,本);
});
});
当您使用jQuery绑定事件处理程序时使用它。1.您的脚本加载不正确。
2.您加载的脚本不在文档范围内 注意:您必须使用单独的脚本标签来加载外部JS文件和编写JS代码。 像这样试试
功能检查(val,即){
var-total=parseInt(0,10);
$('.category_select')。每个(函数(){
如果($(this).is(':checked')==true)
total=total+parseInt($(this).attr('title'),10);
});
如果(总计>val){
$(that.removeAttr('checked');
警报('总计大于val!');
}否则{
警报('Total小于或等于val!');
}
}
复选框:
rails 2中的JQUERY不支持,因此我将脚本更改为Javascript,如下所示
<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>
<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>
函数检查_总计(e,t){
值=parseInt(e);
var a=parseInt(0);
$$(“input.category_select”).each(函数(e){1==e.checked&(a+=parseInt(e.title))),a>值?(警报(“总计大于总计子类别小时数!”),t.checked=!1):a==值和警报(“总计等于总计子类别小时数!”)
谢谢你的回答。上面的脚本对我有效此错误是因为您正在将函数定义到DOM就绪事件的范围内。。。。。
与其这样,不如尝试在函数范围之外定义函数。@HemanthMC,摆脱内联单击处理程序并尝试更新代码我的脚本出了什么问题??你能帮我吗out@HemanthMC,我根本没有修改你的脚本,只是使用jQueryTanks为您的答案绑定了事件,但即使在我尝试了您的脚本之后,我也会收到相同的错误加载您的脚本,其中包含头标记中的
check
函数,而trysorry我没有收到您能帮我如何在头标记中加载该check函数您的html页面中有头标记吗?现在只需在关闭head tag之前将上面的脚本
内容(包括标记)放入其中。我如前所述更新了我的代码,看到您可以更改