javascript 验证复选框(类似于单选按钮),生成scrollTo错误消息
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 验证复选框(类似于单选按钮),生成scrollTo错误消息相关的知识,希望对你有一定的参考价值。
/**
* Copyright (c) 2007-2015 Ariel Flesler - aflesler<a>gmail<d>com | http://flesler.blogspot.com
* Licensed under MIT
* @author Ariel Flesler
* @version 2.1.0
*/
;(function(l){'use strict';l(['jquery'],function($){var k=$.scrollTo=function(a,b,c){return $(window).scrollTo(a,b,c)};k.defaults={axis:'xy',duration:0,limit:true};function isWin(a){return!a.nodeName||$.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!==-1}$.fn.scrollTo=function(f,g,h){if(typeof g==='object'){h=g;g=0}if(typeof h==='function'){h={onAfter:h}}if(f==='max'){f=9e9}h=$.extend({},k.defaults,h);g=g||h.duration;var j=h.queue&&h.axis.length>1;if(j){g/=2}h.offset=both(h.offset);h.over=both(h.over);return this.each(function(){if(f===null)return;var d=isWin(this),elem=d?this.contentWindow||window:this,$elem=$(elem),targ=f,attr={},toff;switch(typeof targ){case'number':case'string':if(/^([+-]=?)?\d+(\.\d+)?(px|%)?$/.test(targ)){targ=both(targ);break}targ=d?$(targ):$(targ,elem);if(!targ.length)return;case'object':if(targ.is||targ.style){toff=(targ=$(targ)).offset()}}var e=$.isFunction(h.offset)&&h.offset(elem,targ)||h.offset;$.each(h.axis.split(''),function(i,a){var b=a==='x'?'Left':'Top',pos=b.toLowerCase(),key='scroll'+b,prev=$elem[key](),max=k.max(elem,a);if(toff){attr[key]=toff[pos]+(d?0:prev-$elem.offset()[pos]);if(h.margin){attr[key]-=parseInt(targ.css('margin'+b),10)||0;attr[key]-=parseInt(targ.css('border'+b+'Width'),10)||0}attr[key]+=e[pos]||0;if(h.over[pos]){attr[key]+=targ[a==='x'?'width':'height']()*h.over[pos]}}else{var c=targ[pos];attr[key]=c.slice&&c.slice(-1)==='%'?parseFloat(c)/100*max:c}if(h.limit&&/^\d+$/.test(attr[key])){attr[key]=attr[key]<=0?0:Math.min(attr[key],max)}if(!i&&h.axis.length>1){if(prev===attr[key]){attr={}}else if(j){animate(h.onAfterFirst);attr={}}}});animate(h.onAfter);function animate(a){var b=$.extend({},h,{queue:true,duration:g,complete:a&&function(){a.call(elem,targ,h)}});$elem.animate(attr,b)}})};k.max=function(a,b){var c=b==='x'?'Width':'Height',scroll='scroll'+c;if(!isWin(a))return a[scroll]-$(a)[c.toLowerCase()]();var d='client'+c,doc=a.ownerDocument||a.document,html=doc.documentElement,body=doc.body;return Math.max(html[scroll],body[scroll])-Math.min(html[d],body[d])};function both(a){return $.isFunction(a)||$.isPlainObject(a)?a:{top:a,left:a}}$.Tween.propHooks.scrollLeft=$.Tween.propHooks.scrollTop={get:function(t){return $(t.elem)[t.prop]()},set:function(t){var a=this.get(t);if(t.options.interrupt&&t._last&&t._last!==a){return $(t.elem).stop()}var b=Math.round(t.now);if(a!==b){$(t.elem)[t.prop](b);t._last=this.get(t)}}};return k})}(typeof define==='function'&&define.amd?define:function(a,b){'use strict';if(typeof module!=='undefined'&&module.exports){module.exports=b(require('jquery'))}else{b(jQuery)}}));
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="https://lp.google-mkto.com/rs/248-TPC-286/images/jquery.scrollTo.min.js"></script>
<script type="text/javascript" src="/js/forms2/js/forms2.js"></script>
<script type="text/javascript">
var $jQ = jQuery.noConflict();
MktoForms2.whenReady(function (form){
$jQ("#Session_1").click(function(){
$jQ("#Session_2").attr("checked",false);
$jQ("#Session_3").attr("checked",false);
$jQ("#Session_4").attr("checked",false);
});
$jQ("#Session_2").click(function(){
$jQ("#Session_1").attr("checked",false);
$jQ("#Session_3").attr("checked",false);
$jQ("#Session_4").attr("checked",false);
});
$jQ("#Session_3").click(function(){
$jQ("#Session_1").attr("checked",false);
$jQ("#Session_2").attr("checked",false);
$jQ("#Session_4").attr("checked",false);
});
$jQ("#Session_4").click(function(){
$jQ("#Session_1").attr("checked",false);
$jQ("#Session_2").attr("checked",false);
$jQ("#Session_3").attr("checked",false);
});
form.onValidate(function(){
if(!$jQ('#Session_1').is(":checked") && !$jQ('#Session_2').is(":checked") && !$jQ('#Session_3').is(":checked") && !$jQ('#Session_4').is(":checked") ){
form.submittable(false);
var checkboxElem = form.getFormElem().find("#Session_1");
form.showErrorMessage("Please make a selection.", checkboxElem);
$jQ(window).scrollTo(checkboxElem,500);
}else{
form.submittable(true);
}
});
});
</script>
以上是关于javascript 验证复选框(类似于单选按钮),生成scrollTo错误消息的主要内容,如果未能解决你的问题,请参考以下文章
像单选按钮一样的复选框,仅限于一个选择,取消选中是不是选中了其他复选框