javascript v html a podmínky If / else
Ahoj kamarádi,
mám prosbu, v JS je pro mne krapet velké moře a já plavu asi blbým směrem....
Vytvářím <form>, který má zobrazovat elemety (input, select atd..) podle toho co v předchozím elementu vybereš.
Takže zkráceně řečeno:
Vyberu si materiál např kov, a v JS nastavím, že jestliže je vybraný 1.řádek, tak v CSS stylech zobraz DIV id="div-material-kov".
HTML:
Kód:
<select id="material" onChange="check1(this);" name="material">
<option value="">Vyberte materiál:</option>
<option value="kov">kov</option>
<option value="plast">plast</option>
<option value="drevo">dřevo</option>
<option value="papir">papír</option>
</select>
<div id="div-material-kov" style="display:none;">
<select id="kov" onChange="check2(this);" name="kov">
<option value="">Jaký typ kovu ?</option>
<option value="hlinik">Hliník</option>
<option value="ocel">Ocel</option>
<option value="zinek">Zinek</option>
</select>
</div>
<div id="div-material-drevo" style="display:none;">
<select id="drevo" onChange="check3(this);" name="drevo">
<option value="">Jaký druh dřeva ?</option>
<option value="buk">Buk</option>
<option value="dub">Dub</option>
<option value="javor">Javor</option>
</select>
</div>
To mi sice funguje, ale pouze za předpokladu, že v JavaScriptu mám jednotlivé funkce pod sebou.
Ovšem pokud je to pod sebou, pak html FORM posílá všechny výsledky a nikoliv jen ty uživatelem vybrané.
SCRIPT:
Kód:
function check1(elem) {
if (elem.selectedIndex == 1) {
document.getElementById("div-material-kov").style.display = 'block';
document.getElementById("div-material-plast").style.display = 'none';
document.getElementById("div-material-drevo").style.display = 'none';
document.getElementById("div-material-papir").style.display = 'none';
} else {
document.getElementById("div-material-kov").style.display = 'none';
}
if (elem.selectedIndex == 3) {
document.getElementById("div-material-kov").style.display = 'none';
document.getElementById("div-material-plast").style.display = 'none';
document.getElementById("div-material-drevo").style.display = 'block';
document.getElementById("div-material-papir").style.display = 'none';
} else {
document.getElementById("div-material-drevo").style.display = 'none';
}
}
function check2(elem) {
if (elem.selectedIndex == 1) {
document.getElementById("div-material-drevo-buk").style.display = 'block';
document.getElementById("div-material-drevo-dub").style.display = 'none';
document.getElementById("div-material-drevo-javor").style.display = 'none';
} else {
document.getElementById("div-material-drevo-buk").style.display = 'none';
}
if (elem.selectedIndex == 2) {
document.getElementById("div-material-drevo-buk").style.display = 'none';
document.getElementById("div-material-drevo-dub").style.display = 'block';
document.getElementById("div-material-drevo-javor").style.display = 'none';
} else {
document.getElementById("div-material-drevo-dub").style.display = 'none';
}
}
Samozřejmě, že je možné, že to celé řeším zbytečně mooooc složitě a že daleko jednodušší by bylo udělat jinak. Ovšem jak?
Přičemž FORM bude ještě obsahovat ALERTY, pokud nějaké políčko nebude zvolené nebo vyplněné.
Re: javascript v html a podmínky If / else
Neber to jako fundovanou radu, nemám v tomhle na to dostatek znalostí. Spís jen jako urcitý obecný povzdech uzivatele, který dostává vyrázku uz jen z nutnosti mít povolený JS, trna hrúzou, co kde zas natropí, kde me nechá dlouhé minuty cekat, aby pak vydal chybovou hlásku, ci co kde úplne zablokuje, co po nem pri prístím bootu budu muset cistit, a jsa si vedom, ze s temihle problémy na svete zdaleka nejsem sám, ze to není snad jen otázka mého - jak tu leckterí mozná vedí - trochu letitého zarízení.
Takze otázka v nadeji, ze ten muj povzdech snad prece jen bude aspon trochu inspirativní, na ty, jez - jak pro kamaráda Atreida doufám;) - mu budou dávat uzitecné rady: Nedalo by se to realizovat bez Javascriptu?
Re: javascript v html a podmínky If / else
Tož tohle samozřejmě nevím, zdali se dá formulář realizovat bez nutnosti JS.
Spíše se ale obávám, že nikoliv, protože samotné html reaguje až po stisku tlačítka. tím provede nějaký další process, zatím co script, ti kontroluje průběžně co uživ dělá.
Re: javascript v html a podmínky If / else
Nojo, to je ovsem taky práve jedna z vecí, ze nezrídka na plose tropí rusivou neplechu - jak clovek po ní jezdí mysí, tu vyskocí okénko, tam vyskocí okénko, zacloní neco, na co clovek zrovna potrebuje koukat, treba i neco, kam zrovna chtel cvaknout a cvakne do toho okénka, címz spustí neco, co vúbec nechtel, pak jindy se musí mysí smrdlat po celé plose, ne-li aktivovat jiná okna a taby, aby vyskocilé okénko zmizelo, aby v zápetí v dúsledku "neopatrného" pohybu po plose se stejnou tvrdosíjností vyskocilo znova nebo jinde podobné. Proste aplikace delaj, co chtej ony a ne vzdy bezpodmínecne to, co chce uzivatel, se kterým jednají mírne receno jako s dusevne zaostalým. Programátor se múze vsemozne snazit, aby se tohle nedelo, spokojený ukoncí svou práci, ze se mu tohle na jeho monitoru - a vúbec dríve zmínené v kompu - snad nedeje, jenze pak prijde uzivatel, který mel dobrý dúvod, aby si v celé té snúsce prohlízecú a systémú, co jsou, vybral a pouzíval trochu jinou konstelaci, nez mel programátor, a je jen otázka casu, kdy bude na pomoc volat Dr. Chocholouska...
Dá se ríct, ze Javascript je nejvýznamnejsí zdroj takovýhle problémú.
A pak jeste prijdou weby, které uzivateli, který ví, proc má JS vypnutý (mj. i bezpecnostní dúvody pro to múzou mluvit), oznámí, ze si ho musí zapnout, jinak mu vsechno nepújde, jak má; a skutecne je pak o mnohé funkce ochuzen, které by mohl mít, kdyby webmaster na Javascriptu netrval. A pak ti jeste lepsí, kterí návstevníka bez zapnutého JS proste vúbec nepustí dál.
Re: javascript v html a podmínky If / else
Jeden form = jeden req = odesle se vzdy vse, co obsahuje. Musel bys to bud rozdelit (a pak mzues js rozhodnout, ktery casti posles), nebo to posilat pomoci js, coz de taky, ale pak to samo uz bez js nebude fungovat. Hledej ajax post ...
Jinak si to sam rozbijis dotazem v option, tam nema co pohledavat. Asi nejlip bys to udelal tak, ze posles neco jako 001014005036 ... tzn volba cislo 1, urcuje vyznam 14 a ta urcuje vyznam 5 atd atd.
Re: javascript v html a podmínky If / else
Tak nakonec jsem to vyřešil, že posílám vše, ale následně v php rozdělím co z toho je použitelné a co ne..
díky