从另一个输入值自动填充输入

时间:2019-12-01 11:42:02

标签: javascript php jquery arrays input

我有问题:

<table>
    <?php
        $data['kriteria'] = array('IPK', 'SEMESTER', 'PENGHASILAN', 'BEASISWA LAIN');
        $kriteria = array();
        foreach ($data['kriteria'] as $key => $val) {
            $kriteria[$key] = $val['nama'];
        }
    ?>
    <thead>
        <tr>
            <th>Kriteria</th>
            <?php
            foreach ($kriteria as $val) {
                echo '<th>' . $val . '</th>';
            }
            ?>
        </tr>
    </thead>
    <tbody>
        <?php $n = count($kriteria); ?>
        <?php for ($i = 0; $i < $n; $i++): ?>
            <tr>
                <th><?= $kriteria[$i] ?></th>
                <?php for ($j = 0; $j < $n; $j++): ?>
                    <td><input type="text"  class="form-control" id="<?= $kriteria[$j] . $kriteria[$i] ?>" name="<?= $j. $i ?>" value=""></td>
                <?php endfor; ?>
            </tr>
        <?php endfor; ?>
    </tbody>
</table>

这是结果

enter image description here

当我从红色侧输入值时,我希望蓝色侧值自动填充 如上图所示 在写问题之前,我试图用jQuery这样自动填充

<script type="text/javascript">
    $(document).ready(function () {
        $("#SI").keyup(function () {
            var value = $(this).val();
            $("#IS").val(1/value);
        });
    });
</script>

但这是行不通的

3 个答案:

答案 0 :(得分:0)

  

谢谢大家的响应,我现在可以解决我的问题   这是代码


<table>
                <?php
                $kriteria = array('IP', 'SE', 'PE', 'BE');

                ?>
            <thead>
                <tr>
                    <th>Kriteria</th>
                    <?php
                    foreach ($kriteria as $val) {
                        echo '<th>' . $val . '</th>';
                    }
                    ?>
                </tr>
            </thead>
            <tbody>
                <?php $n = count($kriteria); ?>
                <?php for ($i = 0; $i < $n; $i++): ?>
                <tr>
                    <th><?= $kriteria[$i] ?></th>
                    <?php for ($j = 0; $j < $n; $j++): ?>
                        <td><input type="text"  class="form-control" id="<?= $kriteria[$j] . $kriteria[$i] ?>" name="<?= $j. $i ?>" value=""></td>
                    <?php endfor; ?>
                </tr>
                <?php endfor; ?>
            </tbody>
        </table>


        <script type="text/javascript">
                $(document).ready(function () {
                    $("#IPIP").val(1);
                    $("#SESE").val(1);
                    $("#PEPE").val(1);
                    $("#BEBE").val(1);
                    $("#SEIP").keyup(function () {
                        var value = $(this).val();
                        $("#IPSE").val(1/value);
                    });
                    $("#PEIP").keyup(function () {
                        var value = $(this).val();
                        $("#IPPE").val(1/value);
                    });
                    $("#BEIP").keyup(function () {
                        var value = $(this).val();
                        $("#IPBE").val(1/value);
                    });
                    $("#PESE").keyup(function () {
                        var value = $(this).val();
                        $("#SEPE").val(1/value);
                    });
                    $("#BESE").keyup(function () {
                        var value = $(this).val();
                        $("#SEBE").val(1/value);
                    });
                    $("#BEPE").keyup(function () {
                        var value = $(this).val();
                        $("#PEBE").val(1/value);
                    });

                });
        </script>
  

我也包含此代码,也可以运行jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 1 :(得分:0)

let

<table>
<?php
$kriteria = array('IP', 'SE', 'PE', 'BE');

?>
<thead>
<tr>
    <th>Kriteria</th>
    <?php
    foreach ($kriteria as $val) {
        echo '<th>' . $val . '</th>';
    }
    ?>
</tr>
</thead>
<tbody>
<?php $n = count($kriteria); ?>
<?php for ($i = 0; $i < $n; $i++): ?>
    <tr>
        <th><?= $kriteria[$i] ?></th>
        <?php for ($j = 0; $j < $n; $j++): ?>
            <td><input type="text" class="form-control" index-j="<?= $j ?>"
                       index-i="<?= $i ?>"
                       id="<?= $kriteria[$j] . $kriteria[$i] ?>"
                       name="<?= $j . $i ?>" value=""></td>
        <?php endfor; ?>
    </tr>
<?php endfor; ?>
</tbody>

这是针对您的答案的更好的解决方案,如果您的数组是动态的并且编码更好,那么它会很合适。

答案 2 :(得分:0)

如果您想进行数学运算并且只想在第一个输入中输入数字,那么您的JQuery是正确的,但是如果您尝试对文本执行相同的操作,则它将返回NaN,因为它假设您要返回“一个除以值”,因此您可以执行以下操作,然后右键单击另一个函数进行数学运算:

$(document).ready(function () {
    $("#SI").keyup(function () {
        var value = $("#SI").val();
        $("#IS").val(value)
    });
});

检查运行中的JSFiddle here