使用<div>标记</div>中的JavaScript显示输入标记

时间:2014-05-22 07:09:46

标签: php jquery

我有关于javascript和级联下拉列表的问题。 所以在主页面上我有3个下拉菜单,然后我会显示下拉菜单的结果。每个下拉列表都有自己的处理页面,然后我们从下拉列表中获取所有值,程序将显示3个带有javascript的输入标签用于值处理。 问题是我无法在显示div时使javascript工作。

我应该将脚本放在主页面还是包含要在主页的div部分显示内容的页面内?

至于我现在把脚本放在内容页面上

在内容页面上,

<!doctype html>
<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.9.1.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>   

脚本已经在jsfiddle中工作,将值分配到3个输入标记中。 我能够显示输入标签,但java脚本不起作用。

请帮帮我......

1 个答案:

答案 0 :(得分:0)

您需要在HTML页面的标记中打印PHP代码:

</head>
<body>
<?php
    while ($row = oci_fetch_array($result, OCI_BOTH)) {
        $qtyAvailable = $row['QTY_REQUIRED'] - $row['QTY_CUT'];   
        echo '<span id="quantityRequired">'.$qtyAvailable.'</span>';
        echo '<input class="form-control" id="cncQty"    name="cncQty"    type="number" min="0" value="0" placeholder="CNC">';         
        echo '<input class="form-control" id="scatorQty" name="scatorQty" type="number" min="0" value="0" placeholder="SCATOR">';       
        echo '<input class="form-control" id="manualQty" name="manualQty" type="number" min="0" value="0" placeholder="MANUAL">';
        echo '<br/>';      
    }
?>
</body>
</html>