Results for tag "checkbox"

多选框全选/取消

今天闲来无事,写了一个多选框全选/取消的效果,代码如下:

function select_all(form_name, all_select_name, checkbox_name)
{
  var check_flag = document.form_name.all_select_name.checked;

  if (document.form_name.elements[checkbox_name]) {
    if (document.form_name.elements[checkbox_name].length == null) {
      if (check_flag == true) {
        document.form_name.elements[checkbox_name].checked = true;
      } else {
        document.form_name.elements[checkbox_name].checked = false;
      }
    } else {
      for (var i = 0; i < document.form_name.elements[checkbox_name].length; i++) {
        if (check_flag == true) {
          document.form_name.elements[checkbox_name][i].checked = true;
        } else {
          document.form_name.elements[checkbox_name][i].checked = false;
        }
      }
    }
  }
}

以上的方法只适用于以下的结构,如下:

<form name=”form1″>
<input type=”checkbox” name=”all_select” onclick=”select_all(‘form1’, ‘all_select’, ‘a[]’);”>
<input type=”checkbox” name=”a[]” value=”1″>
<input type=”checkbox” name=”a[]” value=”2″>
<input type=”checkbox” name=”a[]” value=”3″>
</form>