klavsr Posted June 10, 2008 Report Share Posted June 10, 2008 Sveiki! Ir input lauks <input type="text" name="summa" value="10.50"/> Kā ar parastu linku būtu iespējams "value" palielināt/samazināt par piem. 0.10 ? Value, protams, var būt tikai vesels skaitlis vai daļskaitlis, kā piemērā. Paldies. Link to comment Share on other sites More sharing options...
andrisp Posted June 10, 2008 Report Share Posted June 10, 2008 Pieliec tam linka onclick eventu, kas dara apt. kaut ko šādu: document.getElementById('taa_input_elementa_id').value += 0.10; Link to comment Share on other sites More sharing options...
klavsr Posted June 10, 2008 Author Report Share Posted June 10, 2008 Hmm, šis vienkārši pieliek "0.10" klāt stringam, nevis piepluso... Link to comment Share on other sites More sharing options...
Aleksejs Posted June 10, 2008 Report Share Posted June 10, 2008 Nu, tad kaut kā šitā: document.getElementById('taa_input_elementa_id').value = parseFloat(document.getElementById('taa_input_elementa_id').value)+0.10; Link to comment Share on other sites More sharing options...
klavsr Posted June 10, 2008 Author Report Share Posted June 10, 2008 Šādi darbojas, bet sākas kaut kāda mistika: <input type="text" value="212.90" id="aaa"><input type="button" value="+ 0.10" onClick="document.getElementById('aaa').value = parseFloat(document.getElementById('aaa').value) + 0.10;"/> Kad cipars ir piem "213.2", nākamais ir "213.29999999999998". Līdz tam viss ok. Link to comment Share on other sites More sharing options...
martins256 Posted June 10, 2008 Report Share Posted June 10, 2008 Interesanta problēma :D <script> function asd() { document.getElementById('aaa').value = (Math.round((parseFloat(document.getElementById('aaa').value)+0.1)*10))/10; } </script> <input type="text" value="212.90" id="aaa"> <input type="button" value="+ 0.10" onClick="asd()"/> Link to comment Share on other sites More sharing options...
klavsr Posted June 10, 2008 Author Report Share Posted June 10, 2008 Liels paldies, šis darbojas kā nākas ;) P.S. Vai ir iespējams vienmēr saglabāt aiz komata 2 ciparus? Link to comment Share on other sites More sharing options...
martins256 Posted June 10, 2008 Report Share Posted June 10, 2008 )+0.1)*100))/100; Link to comment Share on other sites More sharing options...
indoom Posted June 10, 2008 Report Share Posted June 10, 2008 (edited) Laikam bija domāts, lai saglabājas divi cipari, tas ir, arī nulles. function makeFloatWithTwoDecimals(num) { var puses = num+''.replace(/,/g,'.').replace(/[^\d.]/g,'').split('.'); return puses[0] + '.' + (puses.length > 1 ? (puses[1]+'00').substr(0,2) : '00'); } Edited June 10, 2008 by indoom Link to comment Share on other sites More sharing options...
Recommended Posts