checkbox勾选判断

	var xieYi=document.getElementById("xieYi");
	if(!xieYi.checked){
		alert("请先阅读并勾选购买协议!");
		return;		
      }


最开始这样写,不过并不是所有的情况都需要勾选这个协议,协议有时不会在前台页面显示,所以改了第二种

    var xieYi=document.getElementById("xieYi");
    if(!xieYi== null && !xieYi.checked){
        alert("请先阅读并勾选购买协议!");
        return;        
    }

当xieYi不存在的时候,xieYi为null,判断xieYi不为null的时候并且xieYi没有勾选的时候就alert。

可惜,这段代码没有发挥预想中的作用。

最后发现是判断xieYi写错了。

最终版:

    var xieYi=document.getElementById("xieYi");
    if(xieYi!= null && !xieYi.checked){
        alert("请先阅读并勾选购买协议!");
        return;        
    }
原文地址:https://www.cnblogs.com/canrz/p/3516455.html