天天看点

获取单选框复选框被选中的值

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>JQuery radio  checkBox</title>
    <script type="text/javascript" language="javascript" src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript" language="javascript">
      $(function() {
        
        $("form").submit(function() {
          //判断单选框是否被选中
          var val = $('input:radio[name="sex"]:checked').val();
          if (val == null) {
            alert("什么也没选中!");
            return false;
          } else {
            alert(val);
          }
          var arr = []; //定义一个数组  
          $('input[name="ll"]:checked').each(function() { //遍历每一个名字为ll的复选框,其中选中的执行函数  
            //1.将选中的文本值添加到数组arr中
            //arr.push($(this).next().html()); 
            //2.将选中的value值添加到数组内
            arr.push($(this).val());
          });
          //遍历数组
          for (var i = 0; i < arr.length; i++) {
            alert(arr[i]);
          }
        })
      });
    </script>
  </head>

  <body>
    <form id="form1">
      <input type="radio" name="sex" value="男" />男
      <input type="radio" name="sex" value="女" />女
      <br />

      <input type="checkbox" name="ll" value="1" /><span>100</span>
      <input type="checkbox" name="ll" value="2" /><span>200</span>
      <input type="checkbox" name="ll" value="3" /><span>300</span>
      <input type="checkbox" name="ll" value="4" /><span>400</span>
      <br/>
      <input type="submit" value="提交" />
    </form>
  </body>

</html>