php - dynamically distribute values into 3 tags -
i have programming problem now. trying dynamically allocate value 3 input tags. basic idea sum of 3 inputs should not exceed value given.
so code this
<!doctype html> <html> <head> <script type="text/javascript" src="../../jquery/jquery-1.11.0.min.js"></script> <script type="text/javascript"> var total = parseint($('#quantityrequired').text()), inputs = $('input[type="number"]'); inputs .attr('max', total) .change(function() { //make sure current value in range if($(this).val() > parseint($(this).attr('max'))) { $(this).val($(this).attr('max')); } else if ($(this).val() < parseint($(this).attr('min'))) { $(this).val($(this).attr('min')); } //get available total var current = available(); //now update max on each input $('input').each(function(indx) { $(this).attr('max', parseint($(this).val()) + total - current); }); }); function available() { var sum = 0; inputs.each(function() { sum += parseint($(this).val()); }); return sum; } </script> </head> <body> <?php $projectname = strval($_get['project']); $thicknessvalue = intval($_get['thicknessvalue']); $baseplatevalue = strval($_get['baseplatevalue']); $query = "select qty_required, qty_cut component thickness = :thicknessval , project_name = :projectname , base_plate = :baseplateval , request_status = 'open'"; $result = oci_parse($conn, $query); oci_bind_by_name($result, ":projectname", $projectname); oci_bind_by_name($result, ":thicknessval", $thicknessvalue); oci_bind_by_name($result, ":baseplateval", $baseplatevalue); oci_execute($result); ?> <?php while ($row = oci_fetch_array($result, oci_both)){ $qtyavailable = $row['qty_required'] - $row['qty_cut']; echo '<span id="quantityrequired">'.$qtyavailable.'</span>'; echo '<input id="cncqty" name="cncqty" type="number" min="0" value="0" placeholder="cnc" required>'; echo '<input id="scatorqty" name="scatorqty" type="number" min="0" value="0" placeholder="scator" required>'; echo '<input id="manualqty" name="manualqty" type="number" min="0" value="0" placeholder="manual" required>'; echo '<br/>'; } ?> </body> </html>
so of code here displayed in div section of other html file. problem code is, jquery doesn't work when displayed in div. tried in jsfilddle , jquery code works well.
and jsfiddle here : http://jsfiddle.net/k2qvv/
but when apply in php code. doesn't work. doing wrong here ?
your code running before page rendered, things like:
inputs = $('input[type="number"]');
won't find anything.
to fix this, wrap javascript other available
function in jquery ready:
$( document ).ready(function() { ... });
Comments
Post a Comment