Autor Zpráva
radas
Profil *
Zdarvím,
potřeboval jsem nějaky js pro hromadné vybirani chceckboxu. nakonec jsem našel, ale ma to haček
js

     function markAllRows( container_id ) {
          var rows = document.getElementById(container_id).getElementsByTagName('input');
          var checkbox;
          for ( var i = 0; i < rows.length; i++ ) {
            checkbox = rows[i];
            if (checkbox.type == 'checkbox' ) {
              checkbox.checked = !checkbox.checked;
            }
          }
          return true;
      }


muj formulař


<form id="check" action="nezarazene.php" method="post">
<input type="checkbox" name="kat_id[]" value="1" /> kategorie
<input type="checkbox" name="id[]" value="1" /> id produktu
<input type="checkbox" name="id_delete[]" value="1" /> smazat

a ja bych potřeboval že když zmačknu vybrat všchny
<input name="id[]" value="" type="checkbox" onclick="markAllRows('check');" />vybrat všechny produkty
tak aby se mi jen označily checkboxy s nazvem id[]
myslel jsem že bude stačit

   function markAllRows( container_id ) {
          var rows = document.getElementById(container_id).getElementsByTagName('input');
          var checkbox;
          for ( var i = 0; i < rows.length; i++ ) {
            checkbox = rows[i];
            if (checkbox.type == 'checkbox' and  checkbox.name== 'id'  ) {
              checkbox.checked = !checkbox.checked;
            }
          }
          return true;
      }

bohužel to nefunguje...

Moderátor Chamurappi: Opravil jsem rozlezlou kurzívu.
Tomashek
Profil
V javascriptu se používá místo AND - &&

     function markAllRows( container_id ) {
          var rows = document.getElementById(container_id).getElementsByName('id');  //zkus změnit z TagNamu na Name
          var checkbox;
          for ( var i = 0; i < rows.length; i++ ) {
            checkbox = rows;
            if (checkbox.type == 'checkbox' ) {
              checkbox.checked = !checkbox.checked;
            }
          }
          return true;
      }

radas
Profil *
nepomohlo..s tím and jsem to našel...ale dík za info...
Tomashek
Profil
function markAllRows(container_id) 
{
var rows,checkbox;
rows = document.getElementById(container_id).elements['id[]'];

for(x = 0;x<rows.length;x++)
{
checkbox = rows[x];
if(checkbox.type=='checkbox'){checkbox.checked = !checkbox.checked;}
}
return true;
}

Tohle mi funguje
radas
Profil *
děkuji mi taky...
matlala
Profil
ahoj,
narazil jsem na tuhle diskusi, ať mi nikdo neříká, že to s tím nesouvisí, ale je tu malej problém, potřebuju ho vyřešit.
Zkusil jsem script a funguje dobře, ale pouze v IE, mozilla, opera atd nejde. Ovšem koukal jsem na scripty phpMyAdmina a tam to je skoro to samé, ovšem funguje to ve všech prohlížečích, při pokusu o zkopírování to nejde, zase pouze IE. Můžete mi někdo poradit jak ten script zprovoznit?

Sám se snažím už 2 hodiny a zatím bez úspěchu.
matlala
Profil
aha hurá tak se mi to podařilo vytáhnout, už to jede, ale musí být v tabulce. Kdyby se to někomu hodilo, tak tohle pojede i v mozille, pracně vytaženo z phpMyAdmin, který funguje ve všech prohlížečích. Možná to jde ještě zkrátit, ale když se JS hodí to vnějšího souboru je to snadno použitelné, hlavní je funkce PMA_markRowsInit():
<script type="text/javascript">
var marked_row = new Array;
/**
 * enables highlight and marking of rows in data tables
 *
 */
function PMA_markRowsInit() {
    // for every table row ...
    var rows = document.getElementsByTagName('tr');
    for ( var i = 0; i < rows.length; i++ ) {
        // ... with the class 'odd' or 'even' ...
        if ( 'odd' != rows[i].className.substr(0,3) && 'even' != rows[i].className.substr(0,4) ) {
            continue;
        }
        // ... add event listeners ...
        // ... to highlight the row on mouseover ...
        if ( navigator.appName == 'Microsoft Internet Explorer' ) {
            // but only for IE, other browsers are handled by :hover in css
            rows[i].onmouseover = function() {
                this.className += ' hover';
            }
            rows[i].onmouseout = function() {
                this.className = this.className.replace( ' hover', '' );
            }
        }
        // Do not set click events if not wanted
        if (rows[i].className.search(/noclick/) != -1) {
            continue;
        }
        // ... and to mark the row on click ...
        rows[i].onmousedown = function() {
            var unique_id;
            var checkbox;

            checkbox = this.getElementsByTagName( 'input' )[0];
            if ( checkbox && checkbox.type == 'checkbox' ) {
                unique_id = checkbox.name + checkbox.value;
            } else if ( this.id.length > 0 ) {
                unique_id = this.id;
            } else {
                return;
            }

            if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                marked_row[unique_id] = true;
            } else {
                marked_row[unique_id] = false;
            }

            if ( marked_row[unique_id] ) {
                this.className += ' marked';
            } else {
                this.className = this.className.replace(' marked', '');
            }

            if ( checkbox && checkbox.disabled == false ) {
                checkbox.checked = marked_row[unique_id];
            }
        }

        // ... and disable label ...
        var labeltag = rows[i].getElementsByTagName('label')[0];
        if ( labeltag ) {
            labeltag.onclick = function() {
                return false;
            }
        }
        // .. and checkbox clicks
        var checkbox = rows[i].getElementsByTagName('input')[0];
        if ( checkbox ) {
            checkbox.onclick = function() {
                // opera does not recognize return false;
                this.checked = ! this.checked;
            }
        }
    }
}
window.onload=PMA_markRowsInit;

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function markAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            if ( checkbox.disabled == false ) {
                checkbox.checked = true;
                if ( typeof(marked_row[unique_id]) == 'undefined' || !marked_row[unique_id] ) {
                    rows[i].className += ' marked';
                    marked_row[unique_id] = true;
                }
            }
        }
    }

    return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element
 * the given element is usaly a table or a div containing the table or tables
 *
 * @param    container    DOM element
 */
function unMarkAllRows( container_id ) {
    var rows = document.getElementById(container_id).getElementsByTagName('tr');
    var unique_id;
    var checkbox;

    for ( var i = 0; i < rows.length; i++ ) {

        checkbox = rows[i].getElementsByTagName( 'input' )[0];

        if ( checkbox && checkbox.type == 'checkbox' ) {
            unique_id = checkbox.name + checkbox.value;
            checkbox.checked = false;
            rows[i].className = rows[i].className.replace(' marked', '');
            marked_row[unique_id] = false;
        }
    }

    return true;
}
</script>
    
<form method="post" action="rows.php" name="soubory" id="souboryvyber">
<table>
<tr><td>
        <input type="checkbox" name="polozka[]">
</td></tr>
<tr><td>
        <input type="checkbox" name="polozka[]">
</td></tr>
<tr><td>
        <input type="checkbox" name="polozka[]">
</td></tr>
<tr><td>
        <input type="checkbox" name="polozka[]">
</td></tr>
<tr><td>
        <input type="checkbox" name="polozka[]">
</td></tr>
</table>
<a href="#" onclick="if (markAllRows('souboryvyber')) return false;">Zaškrtnout vše</a>
<!--místo href="#" doporučuju dát odkaz na ošetřenou verzi, kvůli nefunknosti při vypnutím JS nebo taky možná v mobilu. markAllRows('id_formuláře')-->
 / 
<a href="#" onclick="if (unMarkAllRows('souboryvyber')) return false;">Odškrtnout vše</a>
</form>

Vaše odpověď

Mohlo by se hodit

Neumíte-li správně určit příčinu chyby, vkládejte odkazy na živé ukázky.
Užíváte-li nějakou cizí knihovnu, ukažte odpovídajícím, kde jste ji vzali.

Užitečné odkazy:

Odkud se sem odkazuje


Prosím používejte diakritiku a interpunkci.

Ochrana proti spamu. Napište prosím číslo dvě-sta čtyřicet-sedm:

0