复选框返回选中项ID到后台

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title></title>
    <script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.6.4/jquery.js"></script>
    <script type="text/javascript">
    var index;

    $(function(){
      $("input[name='all']").attr("checked",true);
      var all=$("input[name='local']").attr("checked",true);
    })

    function checkAll(){
      var evt=getEvent();
      var e=evt.srcElement || evt.target;

      if(e.checked){
        var all=$("input[name='local']").attr("checked",true);
      }else{
        var all=$("input[name='local']").attr("checked",false);
      }
      getId();
    }

    function checkItem(){
      getId();
    }

    function getId(){
      arr=new Array();
      var item=document.getElementsByName("local");

    for(i=0;i<item.length;i++){
      if(item[i].checked)
      arr.push(item[i].value)
    }

    alert(arr);

    $.post('test.php',{ //地址
      arr:arr
    },function(data){
      alert(data);
    });
  }
  //IE浏览器支持window.event.srcElement,而firefox支持window.event.target;
  function getEvent()
  {
    if(document.all)
    {
      return window.event;//如果是ie
    }
  func=getEvent.caller;
  while(func!=null)
  {
    var arg0=func.arguments[0];
    if(arg0)
    {
      if((arg0.constructor==Event || arg0.constructor ==MouseEvent)
      ||(typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
      {
        return arg0;
      }
    }
    func=func.caller;
  }
  return null;
}
</script>
</head>
<body>

<label><input name="all" type="checkbox" id="all" value="0" onclick="checkAll()"/>全选 </label>
<label><input name="local" type="checkbox" id="a" value="1" onclick="checkItem()"/>广州站 </label>
<label><input name="local" type="checkbox" id="b" value="2" onclick="checkItem()"/>杭州站</label>
<label><input name="local" type="checkbox" id="c" value="3" onclick="checkItem()"/>普宁站</label>
<label><input name="local" type="checkbox" id="d" value="4" onclick="checkItem()"/>北京站</label>
<label><input name="local" type="checkbox" id="e" value="5" onclick="checkItem()"/>郴州站</label>
<label><input name="local" type="checkbox" id="f" value="6" onclick="checkItem()"/>中山站</label>

</body>
</html>

 

posted @ 2016-12-28 13:25  渣渣菜鸟  阅读(1357)  评论(0编辑  收藏  举报