我现在有一些编程问题。我想要做的是动态地将值分配到3个输入标签中。所以基本思路是这3个输入的总和不应超过给定的值。
所以我的代码就像这样
<!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 that current value is 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 currently 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 FROM COMPONENT
WHERE THICKNESS = :thicknessVal
AND PROJECT_NAME = :projectName
AND BASE_PLATE = :baseplateVal
AND 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>
所以这里的所有代码都将显示在另一个html文件的div部分中。 我的代码的问题是,jquery在div中显示时不起作用。我在jsfilddle中尝试过,jquery代码运行良好。
并且jsfiddle在这里:http://jsfiddle.net/k2QVV/
但是当我在php代码中应用它时。它没有用。我在这做错了什么?
答案 0 :(得分:1)
您的代码在呈现页面之前正在运行,例如:
inputs = $('input[type="number"]');
找不到任何东西。
要解决此问题,请在jQuery ready中包装除available
函数之外的所有javascript:
$( document ).ready(function() {
...
});