(Раз)Маркиране на много checkbox-ве с един клик

He3aBuCuM

Registered
Как мога да направя, така че при клик на линк бутон, да се маркират всички checkbox-ве в дадена форма, както и обратното(размаркиране)?
 
Код:
<script type="text/javascript">

</script>

and here is my checkbox list:

<form name="chb" action="" method="post">
<input type="checkbox" id="chb1" name="chbx" value="1" /> <input type="checkbox" id="chb2" name="chbx" value="2" /> <input type="checkbox" id="chb3" name="chbx" value="3" /> <input type="checkbox" id="chb4" name="chbx" value="4" /> <input type="checkbox" id="chb5" name="chbx" value="5" /><br />
this checks all checkboxes above<input type="checkbox" id="button1" name="button1" value="select all" onclick="return selDeSel('sel')" /><br />
this unchecks all checkboxes above<input type="checkbox" id="button2" name="button2" value="deselect all" onclick="return selDeSel('desel')" /><br />
</form>
 
Бачка си, но валидатора ми вади яко грешки...
Код:
# Line 53, Column 46: Attribute "name" exists, but can not be used for this element.

										<form action="" method="post" name="chb"> 

✉

You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element. This error is often caused by incorrect use of the "Strict" document type with a document that uses frames (e.g. you must use the "Transitional" document type to get the "target" attribute), or by using vendor proprietary extensions such as "marginheight" (this is usually fixed by using CSS to achieve the desired effect instead).

This error may also result if the element itself is not supported in the document type you are using, as an undefined element will have no supported attributes; in this case, see the element-undefined error message for further information.

How to fix: check the spelling and case of the element and attribute, (Remember XHTML is all lower-case) and/or check that they are both allowed in the chosen document type, and/or use CSS instead of this attribute. If you received this error when using the <embed> element to incorporate flash media in a Web page, see the FAQ item on valid flash.
# Error Line 72, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="40" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 80, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="39" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 88, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="37" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 96, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="36" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 104, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="35" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 112, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="34" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

# Error Line 120, Column 58: ID "chbx" already defined

												<td><input type="checkbox" name="chbx[]" id="chbx" value="33" /></td>

✉

An "id" is a unique identifier. Each time this attribute is used in a document it must have a different value. If you are using this attribute as a hook for style sheets it may be more appropriate to use classes (which group elements) than id (which are used to identify exactly one element).
# Info Line 64, Column 57: ID "chbx" first defined here

												<td><input type="checkbox" name="chbx[]" id="chbx" value="41" /></td>

За checkbox-вете използвах id, защото name-a е за изтриване на селектираните неща(PHP).
 
Това го слагаш в някакъв .js файл или го слагаш в <head>:
Код:
function checkall(form_id, checkall_id, checkbox_array_name) {
            if(!document.getElementById) return;
            var frm = document.getElementById(form_id);
            var checkall = document.getElementById(checkall_id);
            if(!(frm && checkall)) return;
            for(var i=0; i<frm.elements.length; i++)
            {
                    var cbx = frm.elements[i];
                    if(cbx.name == checkbox_array_name) cbx.checked = checkall.checked;
            }
        }

Задаваш един checkbox:
Код:
<input type="checkbox" id="all" onclick='javascript:checkall("del", "all", "c[]");' class="checkbox" />
И после всеки checkbox:
Код:
<input type='checkbox' name='c[]' value='3' class="checkbox" />
 
@nicito, нещо не бачка?

#редакция:
Аз се оправих. Намерих си по-хубаво решение. :)
JS:
Код:
function checkUncheckAll(theElement) {
	var theForm = theElement.form, z = 0;
	for(z=0; z<theForm.length;z++){
		if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
			theForm[z].checked = theElement.checked;
		}
	}
}

xHTML:
Код:
<input type="checkbox" name="checkall" onclick="checkUncheckAll(this);" />

И всичко е перфектно. :)
Все пак давам точка на @niki_noki, защото това, което даде, по принцип работи. :)
 
Странно, че не работи при теб, при мен става, може би не си го сложил на правилното място ;)
 

Back
Горе