javascript - document.myform.checkbox.checked without a form -
i know mentioned on question (above) isn't possible, have problem here, have double tripe , on, forms inside page, , need submit form gets data inside previous forms.
warning: started javascript 3 or 4 days ago, used server-side programming.
let's see:
javascript checks if checkboxes checked (several of them):
function keepcount() { var newcount = 0; if (document.iphone3g.iphone3g1.checked) {newcount = newcount + 1;} if (document.iphone3g.iphone3g2.checked) {newcount = newcount + 1;} if (document.iphone3g.iphone3g3.checked) {newcount = newcount + 1;} if (document.iphone3g.iphone3g4.checked) {newcount = newcount + 1;} if (document.iphone3gs.iphone3gs1.checked) {newcount = newcount + 1;} if (document.iphone3gs.iphone3gs2.checked) {newcount = newcount + 1;} if (document.iphone3gs.iphone3gs3.checked) {newcount = newcount + 1;} if (document.iphone3gs.iphone3gs4.checked) {newcount = newcount + 1;} if (document.iphone4.iphone41.checked) {newcount = newcount + 1;} if (document.iphone4.iphone42.checked) {newcount = newcount + 1;} if (document.iphone4.iphone43.checked) {newcount = newcount + 1;} if (document.iphone4.iphone44.checked) {newcount = newcount + 1;} if (newcount > 1){ descontox = 20/100; valorx = (total.value * descontox).tofixed(2); valor.value = (total.value - valorx).tofixed(2); } if (newcount <= 1){ valor.value = ("");} }
this tells me if discount should applied or not, if more 1 checkbox selected apply 20% discount.
if notice, have several "if (document.iphone3g.iphone3g1.checked) { something} interacts this:
<div id="one" class="hiddendiv"> <div class="image"> <img class="large" src="images/iphone3g.png" alt="iphone3g" width="141" height="141" /> </div> <form name="iphone3g"> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3g1" value="50.00"> vidro partido<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3g2" value="59.00"> lcd danificado<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3g3" value="80.00"> substituir capa traseira<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3g4" value="38.00"> botão volume </form> </div> <div id="two" class="hiddendiv"> <div class="image"> <img class="large" src="images/iphone3gs.png" alt="iphone3g" width="141" height="141" /> </div> <form name="iphone3gs"> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3gs1" value="60"> vidro partido 3gs<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3gs2" value="69"> lcd danificado 3gs<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3gs3" value="89"> substituir capa traseira 3gs<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone3gs4" value="45"> botão volume3gs </form> </div> <div id="three" class="hiddendiv"> <div class="image"> <img class="large" src="images/iphone4.png" alt="iphone3g" width="141" height="141" /> </div> <form name="iphone4"> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone41" value="169"> vidro/lcd partido<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone42" value="99"> substituir capa traseira<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone43" value="79"> botão volume<br /> <input onclick="clickch(this);keepcount();" type="checkbox" name="iphone44" value="76"> botão home </form> </div>
i have more 3 of those, can solve question? need submit these data php file.
using jquery, done succinctly.
// select checked checkboxes name attribute starts 'iphone' var newcount = $("input[name^='iphone']:checked").length; if (newcount > 1){ descontox = 20/100; valorx = (total.value * descontox).tofixed(2); valor.value = (total.value - valorx).tofixed(2); } if (newcount <= 1){ valor.value = ("");} }
edit: souza -- appears based on comments have solved issue using single form element. however, wise use more generic approach 1 above, or 1 posted mrtsherman. checking each individual input element name lead code maintenance nightmare.
it require updating both markup , javascript every time new iphone version and/or carrier, platform comes around. more generic approach allow add new checkbox markup using same naming convention , javascript handle without need make changes code.
Comments
Post a Comment